简体   繁体   English

在R中使用mapply运行逻辑模型

[英]Running logistic model using mapply in R

I'm having the following function: 我有以下功能:

logModels <- function(data_idx_list,data)   

{
  x  <-  lapply(data_idx_list, function(m) 
        sparse.model.matrix(~.,data = data[m,]))
  y <- lapply(data_idx_list, function(m) data$earlyR[m])

  logM <- mapply(function(x,y) {
          cv.glmnet(x=x,y=y,family="binomial",alpha=0)
}, x,y) 

return(logM)

}

where x contains 5 samples of data, and y is my dependent variable . 其中x包含5个数据样本,而y是我的因变量 When I'm trying to use mapply I'm getting the following error: 当我尝试使用mapply ,出现以下错误:

Error in glmnet(x, y, weights = weights, offset = offset, lambda = lambda, : number of observations in y (1) not equal to the number of rows of x (100000) glmnet(x,y,权重=权重,偏移量=偏移量,lambda = lambda,中的错误:y(1)中的观察数不等于x的行数(100000)

But, when I'm running the model as follows it works: 但是,当我按以下方式运行模型时,它可以工作:

lm1 = cv.glmnet(x=x[[1]],y=y,family="binomial",alpha=0)

So, I assume I have some trouble with the way I'm approaching x in my mapply function. 因此,我假设我在mapply函数中接近x的方式遇到了一些麻烦。

Your help will be appreciated. 您的帮助将不胜感激。

If I understand your problem correctly, you simply need to wrap y into a list : 如果我正确理解了您的问题,则只需将y包裹到一个list

logM <- mapply(function(x, y) cv.glmnet(x, y, family = "binomial", alpha = 0),
               x, list(y))

Otherwise individual elements of y are passed to consecutive invocations of your glm function. 否则, y各个元素将传递给glm函数的连续调用。

Another, more concise way of writing this: 另一种更简洁的编写方式:

logM <- mapply(cv.glmnet, x = x, y = list(y), family = "binomial", alpha = 0))

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

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