使用效果包时“下标超出范围”

时间:2012-10-16 16:15:35

标签: r

我正在使用效果包来构建一些概率图,显示逻辑回归模型中预测的概率。但是,我得到一个奇怪的错误信息,不知道问题是什么。

当我尝试生成绘图时,出现以下错误。警告不是问题,而是因为我不理解错误信息告诉我的内容。

library(effects)    

dat$won_ping = as.factor(dat$won_ping)

mod2 = glm(won_ping ~ our_bid +
  age_of_oldest_driver2 + 
  credit_type2 + 
  coverage_type2 +
  home_owner2 +
  vehicle_driver_score + 
  currently_insured2 +
  zipcode2,
  data=dat, family=binomial(link="logit"))

> plot(effect("our_bid*vehicle_driver_score", mod2), rescale.axis=FALSE, multiline=TRUE)
Warning message:
In analyze.model(term, mod, xlevels, default.levels) :
  our_bid:vehicle_driver_score does not appear in the model
Error in plot(effect("our_bid*vehicle_driver_score", mod2), rescale.axis = FALSE,  : 
  error in evaluating the argument 'x' in selecting a method for function 'plot': Error in apply(mod.matrix[, components], 1, prod) : 
  subscript out of bounds

这是关于我的数据和我的glm命令的信息:

> str(dat)
'data.frame':   85240 obs. of  71 variables:
 $ our_bid                     : num  155 123 183 98 108 159 98 123 98 200 ...
 $ won_ping                    : Factor w/ 2 levels "0","1": 1 1 2 1 1 1 1 1 1 1 ...
 $ zipcode2                    : Factor w/ 4 levels "1:6999","10000:14849",..: 4 3 2 1 3 2 3 1 2 2 ...
 $ age_of_oldest_driver2       : Factor w/ 4 levels "18 to 21","22 to 25",..: NA 3 NA NA NA NA 3 NA 3 NA ...
 $ currently_insured2          : Factor w/ 2 levels "0","1": 2 1 2 2 1 1 2 2 1 1 ...
 $ credit_type2                : Ord.factor w/ 4 levels "POOR"<"FAIR"<..: 2 3 2 3 2 2 1 3 3 2 ...
 $ coverage_type2              : Factor w/ 4 levels "BASIC","MINIMUM",..: 4 3 3 3 3 3 3 3 4 3 ...
 $ home_owner2                 : Factor w/ 2 levels "0","1": 1 2 2 2 2 2 2 2 2 2 ...
 $ vehicle_driver_score        : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...

最后,这里可能有一些有用的信息:

> sessionInfo()
R version 2.14.0 (2011-10-31)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] effects_2.2-1    colorspace_1.1-1 nnet_7.3-1       MASS_7.3-16      lattice_0.20-0   foreign_0.8-46  

loaded via a namespace (and not attached):
[1] tools_2.14.0

帮助!什么是错误消息?通常情况下,如果“下标超出范围”,则意味着我选择的数据超出了该数据结构的范围,但这根本就不存在。

修改

致@Rowland

正如我上面所说,警告和错误消息是分开的和无关的。假设我拿出zipcode2并运行glm:

mod2 = glm(won_ping ~ our_bid +
  age_of_oldest_driver2 + 
  credit_type2 + 
  coverage_type2 +
  home_owner2 +
  vehicle_driver_score + 
  currently_insured2,
  data=dat, family=binomial(link="logit"))

> plot(effect("our_bid*home_owner2", mod2), rescale.axis=FALSE, multiline=TRUE)
Warning message:
In analyze.model(term, mod, xlevels, default.levels) :
  our_bid:home_owner2 does not appear in the model

这只产生警告,这很好,因为我得到了理想的结果。因此,“”未出现在模型中的事实不是问题,并且不会导致错误消息。

1 个答案:

答案 0 :(得分:0)

试试这个:

with(dat, table(our_bid, vehicle_driver_score))

我怀疑你有一些无人居住的细胞。通过编辑,我假设作为问题的稀疏性似乎不太可能存在于这两个变量中。尽管你的大量案例仍然存在空单元格,但仍然有可能在构建模型时使用所有这些因子变量。