using the output of paste() as an object, within a for loop in R

允我心安 提交于 2019-12-08 04:50:49

问题


I want to construct a matrix of results for 48 GLMs (including model coefficients, R2, etc.). I though in calling the model objects by using a for loop which iteratively call them using paste() and mget() functions, which create a variable with the same name that the model object. Imagine these are the model objects:

var1_ds1_1<-glm(var1~var_ds1)
var1_ds2_1<-glm(var1~var_ds2)
var1_ds3_1<-glm(var1~var_ds3)

My problem arise when I create a name to call the object using paste() and mget() to automatically create an object name identical to the model object name (in this simple example mget(paste ("var1",table.row)) should result in var1_ds1_1, but I cannot extract the model coefficients or any other parameters.

for (tab.row in 1:48) {
     result.matrix[tab.row,]<-mget(paste ("var1_ds",table.row,"_1"))$coef[1] # An example to extract the linear coefficient
}

My question is: How can I automatically generate the names of the model objects to store their parameters into a new result matrix?

The new matrix should be:

Row Variable Dataset Slope P-Value R2
 1  Var 1    1       1.3   0.001   50%
 2  Var 1    2       0.8   0.004   32%   
 .    .      .        .      .      .
48  Var n    n        .      .      .

Thanks in advance.

来源:https://stackoverflow.com/questions/11723551/using-the-output-of-paste-as-an-object-within-a-for-loop-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!