使用lm()函数 [英] ddply with lm() function
问题描述
x1< - c(1:10,1 :
x2 < - c(1:5,1:5,1:5,1:5)
x3< - c(rep(1,5) 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))
生成此错误:
在model.frame.default中出错(公式= mydf $ y〜mydf $ X1 + mydf $ x2,
drop.unused.levels = TRUE):
无效类型(NULL)变量'mydf $ X1'
欣赏您的帮助。
这是您需要做的。
mods = dlply(mydf,。(x3),lm,formula = y〜x1 + x2)
mods是包含回归结果的两个对象的列表。你可以从mod中提取你需要的东西。例如,如果要提取系数,可以写入
coefs = ldply(mods,coef)
这给你
x3(截取)x1 x2
1 1 11.71015 -0.3193146 NA
2 2 21.83969 -1.4677690 NA
编辑。如果你想要 ANOVA
,那么你可以做
ldply(mods ,anova)
x3 Df Sum Sq均值Sq F值Pr(> F)
1 1 1 2.039237 2.039237 0.4450663 0.52345980
2 1 8 36.654982 4.581873 NA NA
3 2 1 43.086916 43.086916 4.4273907 0.06849533
4 2 8 77.855187 9.731898 NA NA
Hi guys 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))
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 = dlply(mydf, .(x3), lm, formula = y ~ x1 + x2)
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
coefs = ldply(mods, coef)
This gives you
x3 (Intercept) x1 x2
1 1 11.71015 -0.3193146 NA
2 2 21.83969 -1.4677690 NA
EDIT. If you want ANOVA
, then you can just do
ldply(mods, anova)
x3 Df Sum Sq Mean Sq F value Pr(>F)
1 1 1 2.039237 2.039237 0.4450663 0.52345980
2 1 8 36.654982 4.581873 NA NA
3 2 1 43.086916 43.086916 4.4273907 0.06849533
4 2 8 77.855187 9.731898 NA NA
这篇关于使用lm()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!