Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环通过列glmer_R_Loops_Glm - Fatal编程技术网

循环通过列glmer

循环通过列glmer,r,loops,glm,R,Loops,Glm,我试图通过在数据集中包含响应变量(dat_prob)的列中循环来运行glmer。我使用的代码如下,改编自另一个stackoverflow问题()的研究代码 他们的代码: dat_y<-(dat[,c(2:1130)]) dat_x<-(dat[,c(1)]) models <- list() # for(i in names(dat_y)){ y <- dat_y[i] model[[i]] = lm( y~dat_x ) } 我已经为此工

我试图通过在数据集中包含响应变量(dat_prob)的列中循环来运行glmer。我使用的代码如下,改编自另一个stackoverflow问题()的研究代码

他们的代码:

dat_y<-(dat[,c(2:1130)])
dat_x<-(dat[,c(1)])
models <- list()
#
for(i in names(dat_y)){
      y <- dat_y[i]
     model[[i]] = lm( y~dat_x )
    }
我已经为此工作了好几个小时,现在透过树林看不见森林了


非常感谢您的帮助。

y您最好将所有内容都放在data.frame中并进行迭代。或者,您可以提前构造公式(无需对数据进行子集),并将其传递给
glmer
函数。您可以使用
sapply
lapply
浏览公式列表。
dat_prob<-(probs[,c(108:188)])
dat_age<-(probs[,c(12)]) 
dat_dist<-(probs[,c(20)]) 
fyearcap=(probs[,c(25)]) 
fstation=(probs[,c(22)]) 
fnetnum=(probs[,c(23)]) 
fdepth=(probs[,c(24)]) 

models <- list() 
#
for(i in names(dat_prob)){
  y <- dat_prob[i]
  y2=as.vector(y)
  model[[i]] = glmer( y ~ dat_age * dat_dist + (1|fyearcap) + (1|fstation)+
  (1|fnetnum)+ (1|fdepth),family=binomial,REML=TRUE )
}
Error in model.frame.default(drop.unused.levels = TRUE, formula = y ~  :
invalid type (list) for variable 'y'
class(as.vector(mtcars[1]))
# [1] "data.frame"
class(mtcars[[1]])
# [1] "numeric"
for(i in names(dat_prob)) {
  my_formula = as.formula(paste(i,
    "~ dat_age * dat_dist + (1|fyearcap) + (1|fstation)+ (1|fnetnum)+ (1|fdepth)"
  ))
  model[[i]] = glmer(my_formula, family = binomial, REML = TRUE)
}