0

I am using Frank Harrell's rms package in R to fit a linear model. When I try to plot the residuals following an ols fit, I get an error saying that an object was not found, despite having successfully included the relevant object/variable in the fit.

str(school.uni)

'data.frame':   18262 obs. of  6 variables:
$ student  : int  206 565 292 602 1391 1230 1292 1111 789 1426 ...
$ entry    : Factor w/ 2 levels "direct","indirect": 1 1 1 1 1 1 1 1 1 1 ...
$ band     : Factor w/ 12 levels "Band45","Band50",..: 1 1 1 1 1 1 1 1 1 1 ...
$ school   : num  5.35 10.8 11.95 12.6 14.3 ...
$ uni      : num  42 32.5 19.5 76.1 29.4 ...
$ school70 : num  -64.7 -59.2 -58 -57.4 -55.7 .

ddist <<- datadist(school.uni)
options( datadist = 'ddist')
fit.ols.all.school.entry <<- ols(uni ~ school70 * entry, x=TRUE, y=TRUE, data=school.uni)
r <- resid( fit.ols.all.school.entry)
xYplot(r ~ fitted(fit.ols.all.school.entry))

The preceding xYplot command produces the expected plot but when I attempt to plot the residuals by entry group, I get an error

xYplot(r ~ fitted(fit.ols.all.school.entry), groups=entry)

Error in eval(expr, envir, enclos) : object 'entry' not found

I would appreciate assistance in finding the problem.

CrimsonDark
  • 661
  • 9
  • 20

1 Answers1

1

You should try this :

xYplot(r ~ fitted(fit.ols.all.school.entry), groups=school.uni$entry)

or you can use attach(school.uni) after loading the data.

Mamoun Benghezal
  • 5,264
  • 7
  • 28
  • 33
  • A scoping problem ... of course! Interestingly, I tried to reproduce the problem with a much smaller data frame but failed. I now see that the reason for my error-replication failure was because I had a global variable of the same name ("groupingfactor") as the grouping factor column in the data set, simply because of the way that I went about constructing the data frame for the test. All very instructive. – CrimsonDark Feb 14 '15 at 10:38