-1

我尝试选择列以进行线性回归。

我试图做这样的事情,但它似乎不起作用

df <- 0
x <- 0
for(i in 1:30){
  reg.A_i <- lm(log(match("A", i, sep="_"))~  log(A_0) + B + C , data=y)
  x <- coef(summary(reg.A_i))
  df <- cbind(df[,1],x)
}

我的数据框有这样的变量:

A_0,  A_1, A_2, A_3 .... A_30, B, C
4

1 回答 1

1

看来你想要这样的东西:

set.seed(42)
#Some data:
dat <- data.frame(A0=rnorm(100, mean=20), 
                  A1=rnorm(100, mean=30), 
                  A2=rnorm(100, mean=40), 
                  B=rnorm(100), C = rnorm(100))

#reshape your data
library(reshape2)
dat2 <- melt(dat, id.vars=c("A0", "B", "C"), value.name="y")

#do the regressions
library(plyr)
dlply(dat2, .(variable), function(df) {fit <- lm(log(y) ~ log(A0) + B + C, data=df)
                                      coef(summary(fit))   
                                      })

# $A1
#                 Estimate  Std. Error    t value     Pr(>|t|)
# (Intercept)  3.323355703 0.173727484 19.1297061 1.613475e-34
# log(A0)      0.024694764 0.057972711  0.4259722 6.710816e-01
# B            0.001001875 0.003545922  0.2825428 7.781356e-01
# C           -0.003843878 0.003045634 -1.2620944 2.099724e-01
# 
# $A2
#                 Estimate  Std. Error    t value     Pr(>|t|)
# (Intercept)  3.903836714 0.145839694 26.7679986 2.589532e-46
# log(A0)     -0.071847318 0.048666580 -1.4763174 1.431314e-01
# B           -0.001431821 0.002976709 -0.4810081 6.316052e-01
# C            0.001999177 0.002556731  0.7819271 4.361817e-01
# 
# attr(,"split_type")
# [1] "data.frame"
# attr(,"split_labels")
# variable
# 1       A1
# 2       A2
于 2013-08-08T16:13:35.807 回答