How can I use ddply function for linear model?
x1 <- c(1:10, 1:10)
x2 <- c(1:5, 1:5, 1:5, 1:5)
x3 <- c(rep(1,5), rep(2,5), rep(1,5), rep(2,5))
set.seed(123)
y <- rnorm(20, 10, 3)
mydf <- data.frame(x1, x2, x3, y)
require(plyr)
ddply(mydf, mydf$x3, .fun = lm(mydf$y ~ mydf$X1 + mydf$x2))
This generates this error:
Error in model.frame.default(formula = mydf$y ~ mydf$X1 + mydf$x2,
drop.unused.levels = TRUE) :
invalid type (NULL) for variable ‘mydf$X1’
Appreciate your help.
Here is what you need to do.
mods is a list of two objects containing the regression results. you can extract what you need from mods. for example, if you want to extract the coefficients, you could write
This gives you
EDIT. If you want
ANOVA, then you can just do