Skip to content

termplot fails for composite non-factor terms (PR#828)

2 messages · John Maindonald, Thomas Lumley

#
I am running R 1.2.1 under Windows 98SE.

termplot() currently fails when there are composite terms, thus:
  > library(mass)
  > data(hills)
  > hills.lm <- lm(time ~ climb + poly(dist, 2), data = hills)
  > termplot(hills.lm)
  Hit <Return> to see next plot: 
  Error in eval(expr, envir, enclos) : Object "dist" not found

The call
  > termplot(hills.lm,data=hills)
does run without generating an error message, i.e. there is a 
problem with the default setting for the parameter 'data'. 
However the x-axis for the term poly(dist,2) is then incorrectly 
labelled as poly(dist,2), rather than dist
The label poly(dist,2) surely belongs as part of the y-axis label.

Here are suggested fixes

1. In the default parameters for the function call change
   data = model.frame(model)
to
   data = get(as.character(model$call$data))

[This does not work if the variable values are specified in the
function call.  It is not clear to me how to fix that.]

2. A few lines from the beginning, replace
    if (is.null(xlabs)) 
        xlabs <- nmt
    if (is.null(ylab)) 
        ylab <- substitute(link(foo), list(foo = formula(model)[[2]]))
with
   if (is.null(xlabs)) 
        xlabs <- unlist(lapply(cn,function(x)
        if(length(x)>1)as.character(x[[2]]) else as.character(x)))
   if (is.null(ylab)) 
        ylab <- paste("Partial for", nmt)

[The current y-label is "link(<yvar>)".  If it is thought necessary
to specify this, it should be additional to e.g., in my example,
"Partial for poly(dist,2)"]

Replace ylab = ylab with 
   ylab = ylab[i]
in the two places (arguments to plot()) where it appears.

John Maindonald               email : john.maindonald@anu.edu.au        
Statistical Consulting Unit,  phone : (6125)3998        
c/o CMA, SMS,                 fax   : (6125)5549  
John Dedman Mathematical Sciences Building
Australian National University
Canberra ACT 0200
Australia

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Sat, 27 Jan 2001 john.maindonald@anu.edu.au wrote:

            
Also, it does not specify where to get() the data: what environment to
use. The reason that the data= argument is present in termplot() is that
we can't tell where to find the data in general.

The situation is better as of R1.2, since we can look up variables in the
environment of the model formula. This would fix not only termplot() but a
number of other prediction-related problems.
This looks like a good idea. 

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._