使用预测函数时,eval中的错误(Expr,Envir,Enclos)



当我尝试在数据集上运行preditive()时,它会不断给我错误 - eval中的错误(Expr,Envir,Enclos):未找到对象'loanrange'

这是数据集的一部分 -

LoanRange Loan.Type N WAFICO WALTV WAOrigRev WAPTValue 1 0-99999 Conventional 109 722.5216 63.55385 6068.239 0.6031879 2 0-99999 FHA 30 696.6348 80.00100 7129.650 0.5623650 3 0-99999 VA 13 698.6986 74.40525 7838.894 0.4892977 4 100000-149999 Conventional 860 731.2333 68.25817 6438.330 0.5962638 5 100000-149999 FHA 285 673.2256 82.42225 8145.068 0.5211495 6 100000-149999 VA 125 704.1686 87.71306 8911.461 0.5020074 7 150000-199999 Conventional 1291 738.7164 70.08944 8125.979 0.6045117 8 150000-199999 FHA 403 672.0891 84.65318 10112.192 0.5199632 9 150000-199999 VA 195 694.1885 90.77495 10909.393 0.5250807 10 200000-249999 Conventional 1162 740.8614 70.65027 8832.563 0.6111419 11 200000-249999 FHA 348 667.6291 85.13457 11013.856 0.5374226 12 200000-249999 VA 221 702.9796 91.76759 11753.642 0.5078298 13 250000-299999 Conventional 948 742.0405 72.22742 9903.160 0.6106858

以下是用于预测计数数据 n 的代码,确定过度分散 -

model2=glm(N~Loan.Type+WAFICO+WALTV+WAOrigRev+WAPTValue, family=quasipoisson(link = "log"), data = DF)
summary(model2)

这是我为创建计数和使用预测函数的序列所做的 -

countaxis <- seq (0,1500,150)
Y <- predict(model2, list(N=countaxis, type = "response")

在此步骤中,我得到了错误 -

Error in eval(expr, envir, enclos) : object 'LoanRange' not found

有人可以指向我的问题。

考虑您要预测的确切预测。您正在提供Npredict函数值(通过countaxis),但实际上,您设置模型的方式,N是您的响应变量,其余变量是预测变量。这就是为什么R要求LoanRange。它实际上需要LoanRange, Loan.Type, ..., WAPTValue的值才能预测N。因此,您需要馈送predict输入,以便该模型尝试预测N

例如,您可以做这样的事情:

# create some fake data to predict N
newdata1 = data.frame(rbind(c("0-99999", "Conventional", 722.5216, 63.55385,  6068.239, 0.6031879),
                            c("150000-199999", "VA", 12.5216, 3.55385,  60.239, 0.0031879)))
colnames(newdata1) = c("LoanRange" ,"Loan.Type", "WAFICO"  ,"WALTV" , "WAOrigRev" ,"WAPTValue")
# ensure that numeric variables are indeed numeric and not factors
newdata1$WAFICO = as.numeric(as.character(newdata1$WAFICO))
newdata1$WALTV = as.numeric(as.character(newdata1$WALTV))
newdata1$WAPTValue = as.numeric(as.character(newdata1$WAPTValue))
newdata1$WAOrigRev = as.numeric(as.character(newdata1$WAOrigRev))
# make predictions - this will output values of N
predict(model2, newdata = newdata1, type = "response")

最新更新