使用Predict函数时,eval(expr,envir,enclos)出错

时间:2016-12-13 22:08:14

标签: regression glm poisson

当我尝试在数据集上运行predict()时,它会一直给我错误 - eval中的错误(expr,envir,enclos):object' 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

有人可以指出我这里的问题在哪里。

1 个答案:

答案 0 :(得分:0)

想想你究竟想要预测的是什么。您提供的predict函数值为N(通过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")