Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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
glm`predict()`错误:没有适用于';预测';适用于“类”的对象;列表“;_R_Regression_Glm_Predict - Fatal编程技术网

glm`predict()`错误:没有适用于';预测';适用于“类”的对象;列表“;

glm`predict()`错误:没有适用于';预测';适用于“类”的对象;列表“;,r,regression,glm,predict,R,Regression,Glm,Predict,我在控制输入预测函数的对象类型时遇到问题。下面是我的简化函数,它生成glm对象 fitOneSample <- function(x,data,sampleSet) { #how big of a set are we going to analyze? Pick a number between 5,000 & 30,000, then select that many rows to study sampleIndices <- 1:5000 #now ra

我在控制输入预测函数的对象类型时遇到问题。下面是我的简化函数,它生成
glm
对象

fitOneSample <- function(x,data,sampleSet)
{
  #how big of a set are we going to analyze? Pick a number between 5,000 & 30,000, then select that many rows to study
  sampleIndices <- 1:5000

  #now randomly pick which columns to study
  colIndices <- 1:10

  xnames <- paste(names(data[,colIndices]),sep = "")
  formula <- as.formula(paste("target ~ ", paste(xnames,collapse = "+")))
  glm(formula,family=binomial(link=logit),data[sampleIndices,])
}

myFit <- fitOneSample(1,data,sampleSet)
fits <- sapply(1:2,fitOneSample,data,sampleSet)
all.equal(myFit,fits[,1]) #different object types

#this works
probability <- predict(myFit,newdata = data)

#this doesn't
probability2 <- predict(fits[,1],newdata = data)
# Error in UseMethod("predict") :
# no applicable method for 'predict' applied to an object of class "list"

fitOneSample我想我现在可以恢复你的状况了

fits <- sapply(names(trees),
               function (y) do.call(lm, list(formula = paste0(y, " ~ ."), data = trees)))
由于我们使用了
sapply
,每次迭代都返回相同的
lm
对象或长度为12的列表,因此结果将简化为
12*3
矩阵:

class(fits)
# "matrix"

dim(fits)
# 12  3
矩阵索引
fits[,1]
有效

如果您选中
str(fits[,1])
,它看起来几乎像一个正常的
lm
对象。但如果您进一步检查:

class(fits[, 1])
# "list"
Em?它没有“lm”类因此,
S3
分派方法在调用通用函数
predict
时将失败:

predict(x)
#Error in UseMethod("predict") : 
#  no applicable method for 'predict' applied to an object of class "list"

这可以看作是一个很好的例子,说明
sapply
具有破坏性。
我们想要
lappy
,或者至少,
sapply(…,simplify=FALSE)


您可以通过以下方式修复代码:

fits <- lapply(1:2,fitOneSample,data,sampleSet)
probability2 <-predict(fits[[1]],newdata = data)

fits试试
fits谢谢@cryo111。很好,谢谢你。我的主要问题是在循环期间保留结构。你的评论让我意识到sapply破坏了结构-很高兴知道。我修改了先前的函数以使用lappy。
fits <- lapply(names(trees),
               function (y) do.call(lm, list(formula = paste0(y, " ~ ."), data = trees)))
class(fits[[1]])
# "lm"

predict(fits[[1]])
#        1         2         3         4         5         6         7         8 
# 9.642878  9.870295  9.941744 10.742507 10.801587 10.886282 10.859264 10.957380 
#        9        10        11        12        13        14        15        16 
#11.588754 11.289186 11.946525 11.458400 11.536472 11.835338 11.133042 11.783583 
#       17        18        19        20        21        22        23        24 
#13.547349 12.252715 12.603162 12.765403 14.002360 13.364889 14.535617 15.016944 
#       25        26        27        28        29        30        31 
#15.628799 17.945166 17.958236 18.556671 17.229448 17.131858 21.888147 
fits <- lapply(1:2,fitOneSample,data,sampleSet)
probability2 <-predict(fits[[1]],newdata = data)