本文介绍了使用lm()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好,我如何使用ddply函数进行线性模型: x1< - c(1:10,1 : x2 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 writecoefs = ldply(mods, coef)This gives you x3 (Intercept) x1 x21 1 11.71015 -0.3193146 NA2 2 21.83969 -1.4677690 NAEDIT. 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.523459802 1 8 36.654982 4.581873 NA NA3 2 1 43.086916 43.086916 4.4273907 0.068495334 2 8 77.855187 9.731898 NA NA 这篇关于使用lm()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-10 01:34