简体   繁体   English

使用for循环或套用运行多个GAMM模型

[英]Running multiple GAMM models using for loop or lapply

Can someone please help me with running multiple GAMM models in a for loop or lapply: I have a set of 10 response and 20 predictor variables in a large data frame arranged in columns. 有人可以帮我在for循环中或lapply中运行多个GAMM模型:在按列排列的大型数据框中,我有一组10个响应和20个预测变量。

I'd like to apply GAMM model for each predictor-response combination, and summarize their coefficients and significance tests in a table. 我想将GAMM模型应用于每种预测变量-响应组合,并在表格中总结其系数和显着性检验。

models<-gamm(AnimalCount ~ s(temperature), data=dat,family=poisson(link=log) , random=list(Province=~1) ) 模型<-gamm(AnimalCount〜s(温度),data = dat,family = poisson(link = log),random = list(省=〜1))

I think one way to do this is to create a "matrix" list where the number of rows and columns corresponds to the number of responses (i) and predictors (j), respectively. 我认为一种方法是创建一个“矩阵”列表,其中行和列的数量分别对应于响应(i)和预测变量(j)的数量。 Then you can store each model result in the cell[i, j]. 然后,您可以将每个模型结果存储在单元格[i,j]中。 Let me illustrate: 让我说明一下:

## make up some data
library(mgcv)
set.seed(0) 
dat <- gamSim(1,n=200,scale=2)
set.seed(1)
dat2 <- gamSim(1,n=200,scale=2)
names(dat2)[1:5] <- c("y1", paste0("x", 4:7))
d <- cbind(dat[, 1:5], dat2[, 1:5])

Now the made-up data has 2 responses (y, y1) and 8 predictors (x0 ~ x7). 现在,组合数据具有2个响应(y,y1)和8个预测变量(x0〜x7)。 I think you can simplify the process by storing the responses and predictors in separate data frames: 我认为您可以通过将响应和预测变量存储在单独的数据框中来简化过程:

d_resp <- d[ c("y", "y1")]
d_pred <- d[, !(colnames(d) %in% c("y", "y1"))]

## create a "matrix" list of dimensions i x j
results_m <- vector("list", length=ncol(d_resp)*ncol(d_pred))
dim(results_m) <- c(ncol(d_resp), ncol(d_pred))

for(i in 1:ncol(d_resp)){
  for(j in 1:ncol(d_pred)){
    results_m[i, j][[1]] <- gamm(d_resp[, i] ~ s(d_pred[, j]))
  }
}

# flatten the "matrix" list
results_l <- do.call("list", results_m)

You can use sapply/lapply to create a data frame to summarize coefficients, etc. Say, you want to extract fixed-effect intercepts and slopes and stored in a data frame. 您可以使用sapply / lapply创建数据框以汇总系数等。假设您要提取固定效果的截距和斜率并存储在数据框中。

data.frame(t(sapply(results_l, function(l) l$lme$coef$fixed)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM