On 17/05/2012 09:25, David A Vavra wrote:
I've been having a problem using the step function to evaluate models. I've
simplified the code and get the same problem using the dataset Titanic. The
relevant code and output is below. The problem disappears (i.e., 'step' runs
correctly) if I rerun the code but change the 'loglm' call to explicitly
reference Titanic instead of X (as in: loglm(as.formula(Y),data=Titanic)).
What is causing this?
A lack of understanding of 'non-standard evaluation'. X (or at least,
the X you want) is not visible from the standard search path.
TIA,
DAV
----------
catn<-function(...) cat(...,"\n")
local({ X<-Titanic; print(class(X));
Y<-paste('~',paste(names(dimnames(X)),collapse="*"));
print(Y);
sm<-loglm(as.formula(Y),data=X);
catn("SM"); print(sm); catn('running');
step(sm,direction='backward') })
Which will tell you
Error in eval(expr, envir, enclos) : could not find function "loglm"
If you correct that and use a vanilla session you will get
Error in loglm(formula = ~Class + Sex + Age + Survived + Class:Sex +
Class:Age + :
object 'X' not found
which is more informative.
So the solution is to
- use less easily masked names than 'X'.
- ensure the data object is visible on the search path.
Output:
[1] "table"
[1] "~ Class*Sex*Age*Survived"
SM
Call:
loglm(formula = as.formula(Y), data = X)
Statistics:
X^2 df P(> X^2)
Likelihood Ratio 0 0 1
Pearson NaN 0 1
running
Start: AIC=64
~Class * Sex * Age * Survived
Error in loglin(data, margins, start = start, fit = fitted, param = param,
:
subscript out of bounds
Enter a frame number, or 0 to exit
1: local({
X<- Titanic
print(class(X))
Y<- paste("~", paste(names(dimnames(X)), collapse = "*"))
print(Y)
sm<- loglm(as.formula(Y), data = X
2: eval.parent(substitute(eval(quote(expr), envir)))
3: eval(expr, p)
4: eval(expr, envir, enclos)
5: eval(quote({
X<- Titanic
print(class(X))
Y<- paste("~", paste(names(dimnames(X)), collapse = "*"))
print(Y)
sm<- loglm(as.formula(Y), dat
6: eval(expr, envir, enclos)
7: #1: step(sm, direction = "backward")
8: #1: drop1(fit, scope$drop, scale = scale, trace = trace, k = k, ...)
9: #1: drop1.default(fit, scope$drop, scale = scale, trace = trace, k = k,
...)
10: #1: update(object, as.formula(paste("~ . -", tt)), evaluate = FALSE)
11: #1: update.loglm(object, as.formula(paste("~ . -", tt)), evaluate =
FALSE)
12: #1: eval.parent(call)
13: #1: eval(expr, p)
14: #1: eval(expr, envir, enclos)
15: #1: loglm(formula = ~Class + Sex + Age + Survived + Class:Sex +
Class:Age + Sex:Age + Class:Survived + Sex:Survived + Age:Survived +
Class:Sex:Age + Class:
16: #1: loglm1(formula, data, ..., .call = .call, .formula = .formula)
17: #1: loglm1.default(formula, data, ..., .call = .call, .formula =
.formula)
18: #1: loglin(data, margins, start = start, fit = fitted, param = param,
eps = eps, iter = iter, print = print)
Selection: 0
______________________________________________
R-help@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.
--
Brian D. Ripley, rip...@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
______________________________________________
R-help@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.