如何使用R计算N = 1000 MLR模型拟合中每个系数的平均值和标准差? 这是我的功能:
simfun <- function(a=56.25102409,b=1.78977412,c=0.08664925,n=18,x1.sd=18.87671,x2.sd=18.87671,e.sd=18.87671) {
X1 <- rnorm(n, mean=0, sd=x1.sd)
X2 <- rnorm(n, mean=0, sd=x2.sd)
e <- rnorm(n, mean=0, sd=e.sd)
Z <- a+b*X1+c*X2+e
data.frame(X1,X2,Z)
}
statfun <- function(samples) {
coef(lm(Z~X1+X2,data=samples))
}
library(plyr)
raply(1000,statfun(simfun()))
答案 0 :(得分:0)
res&lt; - .Last.value
> apply(res,2,mean)
(Intercept) X1 X2
57.9515278 1.6696702 0.1116194
> apply(res,2,sd)
(Intercept) X1 X2
2.5550134 0.3177064 0.2789701