【问题标题】:Looping Regression extraction for each regression每个回归的循环回归提取
【发布时间】:2013-03-26 05:26:56
【问题描述】:

我可以看到这已经几乎完成了,但我是 R 新手,无法弄清楚。几乎,我有一个回归循环(请不要批评数据挖掘),我需要将每个循环的一些内容报告到一个新的列表/数据框/最合适的内容中。这是我的代码:

#Required packages
require(lattice)
require(plyr)


ACDn <- "ACDn.csv"
x <- as.matrix(read.csv(ACDn, colClasses = "numeric"))

#To find which columns are which, in order to split up the overall dataset.
which( colnames(X)=="H95.H30" )
which( colnames(X)=="H99" )

#Here i split all the data into their different types, i.e. LHt = Lidar Heights. Please ignore
#those that are unpopulated, as i am waiting on data to run.

Yall <- x[,c(59:79)]                            #All "True Variables" - BA, MTH, etc.
Y <- Yall[,10]                                  #Specifies which columnn is the Y variable, BA = 10,
                                                #TopHt = 11, SPH = 12, Vol_live = 13, RecovVol = 14

X <- x[,c(1:58,80:95)]                          #All Lidar metrics and combinations.
LHt <- X[,c(28:41,59:74)]
LCv <- X[,c()]
LKu <- X[,c()]
LSk <- X[,c()]
L?? <- X[,c()]

#Create List file. I 

Optmod1 <- 

#Loop Creation, need dataset sizes. The ?? are caused by not knowing the exact sizes 
#of the relative datasets yet. Somewhere in here i would like the an entry for EACH model to be
#appended to a data.frame (or list, whatever is most appropriate), which would state the variables
# i.e. 'y', 'i', 'j', 'k', 'l', 'm', and the Adj. R-squared value (which i guess can be extracted
# through using 'summary(mod)$adj.r.squared). 

For(i in 1:30) {
  For(j in 1:??) {
    For(k in 1:??) {
      For(l in 1:??){
        For(m in 1:??){
          mod <- lm(Y ~ LHt[i] + LCv[j] + LKu[k] + LSk[l] + L??[m])
        }
      }
    }
  }
}

差不多了,在'mod'每次运行之后,我只需要它抛出'Y'、'i'、'j'、'k'、'l'、'm'和调整后的值。 R-Squared(我猜是通过使用“summary(mod)$adj.r.squared”)到一个可提取的表格中。

对不起,如果其中任何一个是不识字的,我是新手,并且之前刚刚获得过规定的代码,因此我的基本理解很稀疏。

感谢您的宝贵时间!

附:随时提出任何问题 - 我会尽力回答!

【问题讨论】:

    标签: r loops regression extraction


    【解决方案1】:

    您的问题的简短答案是

    Answers = list()
    For(i in 1:30) {
      For(j in 1:??) {
        For(k in 1:??) {
          For(l in 1:??){
            For(m in 1:??){
              mod <- lm(Y ~ LHt[i] + LCv[j] + LKu[k] + LSk[l] + L??[m])
              Answers[[length(Answers)+1]] = list(i,j,k,l,m,summary(mod)$adj.r.squared)
            }
          }
        }
      }
    }
    

    它将您想要的信息存储在列表中。它通过创建一个空白列表来工作,然后在每次循环运行回归模型时附加到该列表。但是,在循环中增长这样的列表是非常糟糕的 R 实践。

    您最好先将LHt[i] + LCv[j] + LKu[k] + LSk[l] + L??[m] 形式的所有可能公式写入一个列表,然后使用 lapply 进行回归...

    首先使用expand.grid给出一个有5列的数据框,每列包含每个类别的一个变量名

    LHT_names = lapply(1:30,function(i) paste("LHt[",i,"]",sep="")) #a list of names of LHT type variables for use in formula
    LCv_names = lapply(1:?,function(i) paste("LCv[",i,"]",sep="")) #similar for LCv
    LKu_names = ...
    LSk_names = ...
    L??_names = ...
    
    temp = expand.grid(c(LHt_names, LCv_names, LKu_names, LSk_names, L??_names))
    

    然后,使用 paste 和 lapply 获取公式列表:

    list_of_formulas = lapply(seq_along(nrow(temp)), function(i) paste("Y~",paste(temp[i,],collapse="+"),sep = ""))
    

    然后,使用 lapply 获取回归模型列表

    list_of_models = lapply(list_of_formulas, function(x)  lm(x) )
    

    【讨论】:

    • 非常感谢您的回复。我的问题是:当我做你说的第二件事(更好的做法)时,我收到一条错误消息:> LHT_names = lapply(1:30,paste("LHt[",i,"]",sep="") ) get(as.character(FUN), mode = "function", envir = envir) 中的错误:找不到模式'function'的对象'LHt[1]' 我该怎么办?
    • 抱歉,打错了……您需要在paste 之前插入function(i)。查看?lapply,并查看该行在工作时的输出。
    • Hrmmmm,我到了最后一段代码,得到一个错误,说function(x) 后面的“=”是意外的。
    • 如果我尝试运行第一段代码:for(i in 1:30) { for(j in 1:17) { for(k in 1:2) { for(l in 1:2){ mod &lt;- lm(Y ~ LHt[i] + LCv[j] + LKu[k] + LSk[l]) Answers[[length(Answers)+1]] = list(i,j,k,l,summary(mod)$adj.r.squared) } } } } 我收到以下错误代码:model.frame.default 中的错误(公式 = Y ~ LHt[i] + LCv[j ] + LKu[k] + : 可变长度不同(为 'LHt[i]' 找到)。我不知道发生了什么哈哈,我刚开始使用这个程序 3 周前,这个学习曲线非常陡峭. (另外,再次感谢您帮助我)。
    • 再检查一遍,那是另一个错字,我今天显然不能写 lapply 语法。
    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2016-01-17
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多