创建一个for循环,其中我按R中的列标识数据 [英] Creating a for-loop where i identifies data by column in R
问题描述
我正在尝试使用一个for循环来创建一系列GAM模型的摘要,这些模型使用一年中的平滑术语(DOY)和一个结合了天气的线性预测变量(有其中有很多).我做了一个运行和输出模型摘要的函数:
gamlin<-function(x){m<-gam(Log10E〜s(DOY)+ x,data = eggseasongam)返回(摘要(m))}
我认为我想在一个for循环中利用以上内容,该循环将第4到173列中的天气预报作为x,但是要努力做到这一点.任何建议将不胜感激.
谢谢,迈克
我头顶上的2种方法可能是:
t< -data.frame(c1 = rnorm(10),c2 = rnorm(10),c3 = rnorm(10))
1-使用 formula
,它允许您将字符串作为公式提供
>f< -function(d,x)lm(公式(paste("c1",x,sep =〜")),data = d)>f(t,"c2")称呼:lm(公式=公式(paste("c1",x,sep =〜")),数据= d)系数:(截取)c2-0.1567 -0.4654
2-或者直接在列中输入
>f2 <函数(d,x)lm(c1〜d [,x],data = d)>f2(t,"c2")称呼:lm(公式= c1〜d [,x],数据= d)系数:(截取)d [,x]-0.1567 -0.4654
一旦确定了动态回归函数的方法,就可以将其置于for循环中
for(col in 4:173)f(t,col)
可能是,您想以某种方式使用结果,而不是仅打印摘要(也许取系数并将其放入数据帧中并对其进行图形绘制,或沿这些方式绘制图形.在这种情况下,我建议签出 plyr
和 reshape
包,具体来说,每个包中的函数 melt
和 ddply
在这种情况下,您可以执行以下操作
库(融化)图书馆(plyr)yvar< -t [,1]xvars< -melt(t [,-1])>头(xvars)可变值1立方厘米-0.82002632立方厘米-1.53592203立方厘米-0.21079134 c2 -0.29502635 c2 0.82319896 c2 -0.5971358betas <-ddply(xvars,.(variable),summary,beta = coefficients(lm(yvar〜value))[2])
I am trying to use a for-loop to create a series of summaries of GAM models of variation in capture rate of an invertebrate species using a smoothed term for day of year (DOY), and a linear predictor incorporating weather (there are many of these). I have made a function to run and output a model summary:
gamlin <- function(x) {
m <- gam(Log10E ~ s(DOY) + x, data=eggseasongam)
return(summary(m))
}
I think that I want to utilize the above in a for-loop that will sequentially take as x the weather predictors in columns 4 through 173, but am struggling to do this. Any suggestions would be very much appreciated.
Thanks, Mike
2 approaches off the top of my head could be:
t<-data.frame(c1=rnorm(10),c2=rnorm(10), c3=rnorm(10))
1- use formula
, which allows you to provide a character string as a formula
> f<-function(d,x) lm(formula(paste("c1",x,sep="~")), data=d)
> f(t,"c2")
Call:
lm(formula = formula(paste("c1", x, sep = "~")), data = d)
Coefficients:
(Intercept) c2
-0.1567 -0.4654
2- Alternatively feed in the column directly
> f2<-function(d,x) lm(c1 ~ d[,x], data=d)
> f2(t,"c2")
Call:
lm(formula = c1 ~ d[, x], data = d)
Coefficients:
(Intercept) d[, x]
-0.1567 -0.4654
Once you have settled on an approach for a dynamic regression function you can put it in a for-loop
for(col in 4:173) f(t,col)
Chances are, you want to use the results somehow, rather than just print a summary (Perhaps take the coefficients and put them in a dataframe and graph them, or something along those lines. In which case I suggest checking out plyr
and reshape
packages and specficially the functions melt
and ddply
within each
In this case you could do as follows
library(melt)
library(plyr)
yvar<-t[,1]
xvars<-melt(t[,-1])
> head(xvars)
variable value
1 c2 -0.8200263
2 c2 -1.5359220
3 c2 -0.2107913
4 c2 -0.2950263
5 c2 0.8231989
6 c2 -0.5971358
betas<-ddply(xvars, .(variable), summarize, beta=coefficients(lm(yvar ~ value))[2])
这篇关于创建一个for循环,其中我按R中的列标识数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!