[R] pR2 stumped

Achim Zeileis Achim.Zeileis at uibk.ac.at
Tue May 7 09:47:41 CEST 2013


On Mon, 6 May 2013, ivo welch wrote:

> Dear R experts:  I am stumped.    I am trying to pick off the mcfadden
> R^2 for a probit.  simple, me thinks---except my code works only in my
> main program, but not in my sub!?    I am probably doing something
> obviously wrong, but I have stared at my code for a while now and I
> feel even more stupid than usually.  am I doing something obviously
> wrong here?  advice, as always, appreciated.  /iaw

pscl:::pR2.polr calls update(object, ~ 1) to obtain the fitted 
log-likelihood of the null model (intercept only). If the calls are nested 
deeply enough, re-evaluating the model call does not work because it 
happens in the wrong environment.

A workaround could be to do the updating within your summarize() function 
directly and then call pscl:::pR2Work(llh, llhNull, n) directly. Or if you 
really just need the McFadden pseudo R-squared, it is then probably 
simplest to compute 1 - llh/llhNull yourself...

Best,
Z

>
> library(MASS)
> library(pscl)
>
> summarize <- function(p) {
>  pp <- polr( factor(r1) ~ factor(r2), Hess=TRUE, data=p, method="probit" )
>  print(pp)
>  cat("---------------- (sub) now we do the pseudo R^2\n")
>  print(pR2(pp))  ## it fails in the summarize sub program, wanting 'p'  huh?
>  cat("---------------- ok\n")
> }
>
> ################################################################
> d <- data.frame(r1=factor( c("a", "a", "b", "b", "c", "c") ),
> r2=factor( c("a", "b", "c", "b", "c", "a") ))
> ################################################################
>
> ### it works in the main program
> pp <- (polr( factor(r1) ~ factor(r2), Hess=TRUE, data=d, method="probit" ))
> print(pp)
> cat("---------------- (main) now we do the pseudo R^2\n")
> print(pR2(pp))
> cat("---------------- ok\n")
> summarize(d)
>
> Error in eval(expr, envir, enclos) : object 'p' not found.
>
> ----
> Ivo Welch (ivo.welch at gmail.com)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list