[R] hypothetical prediction after polr

Xu Jun junxu.r at gmail.com
Wed Oct 19 03:49:09 CEST 2011


Dear R-Help listers,

I am trying to estimate an proportional odds logistic regression model
(or ordered logistic regression) and then make predictions by
supplying a hypothetical x vector. However, somehow this does not
work. I guess I must have missed something here. I first used the polr
function in the MASS package, and I create a data frame and supply it
to the predict function (see below):

###############################################################
myologit <- polr(factor(warm) ~ yr89 + male + white + age + ed + prst,
              data=ordwarm2, method=c("logistic"))
yr89  <- c(1)
male  <- c(1)
white <- c(1)
  age <- c(mean(ordwarm2$age))
  ed  <- c(mean(ordwarm2$ed))
prst  <- c(mean(ordwarm2$prst))
prdata <- data.frame(yr89, male, white, age, ed, prst)

prdata$rankR <-predict(myologit,newdata=prdata,type="probs")
################################################################

I do not have any problem estimating the model, but when it comes to
the last time (predict), I got the following message:

Error in `$<-.data.frame`(`*tmp*`, "rankR", value = c(0.124294963178868,  :
 replacement has 4 rows, data has 1

Can anyone help me out with this error here? Thanks a lot!

Jun Xu, Phd
Assistant Professor
Department of Sociology
Ball State University

P.S.: below is the detailed output from R

################################################################

Call:
polr(formula = factor(warm) ~ yr89 + male + white + age + ed +
   prst, data = ordwarm2, method = c("logistic"))

Coefficients:
         Value Std. Error t value
yr89   0.523912   0.079899   6.557
male  -0.733309   0.078483  -9.344
white -0.391140   0.118381  -3.304
age   -0.021666   0.002469  -8.777
ed     0.067176   0.015975   4.205
prst   0.006072   0.003293   1.844

Intercepts:
   Value    Std. Error t value
1|2  -2.4654   0.2389   -10.3188
2|3  -0.6309   0.2333    -2.7042
3|4   1.2618   0.2340     5.3919

Residual Deviance: 5689.825
AIC: 5707.825

> # predictions for hypothetical data points
> # white male in year 89
> yr89  <- c(1)
> male  <- c(1)
> white <- c(1)
>   age <- c(mean(ordwarm2$age))
>   ed  <- c(mean(ordwarm2$ed))
> prst  <- c(mean(ordwarm2$prst))
> prdata <- data.frame(yr89, male, white, age, ed, prst)
>
> prdata$rankR <-predict(myologit,newdata=prdata,type="probs")
Error in `$<-.data.frame`(`*tmp*`, "rankR", value = c(0.124294963178868,  :
 replacement has 4 rows, data has 1
>
> prdata
 yr89 male white      age       ed     prst
1    1    1     1 44.93546 12.21805 39.58526
###############################################################3



More information about the R-help mailing list