step function stops with "subscript out of bounds"
Thanks. I guess I still don't understand what's going on. It's not at all intuitive that the table used should be in the search path. Why is it searching for the table? Isn't the table already stored in the model? If the documentation says this, I haven't found it. Needing to know what names I can't use is a bit disconcerting. I can think of names with high probability of uniqueness but how could I ever be sure? Table "X" is built on the fly in a script. What I supplied here was a portion of the remaining script. I suppose the solution is to store it in an environment in the search path. DAV -----Original Message----- From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] Sent: Thursday, May 17, 2012 6:46 AM To: David A Vavra Cc: r-help at r-project.org Subject: Re: [R] step function stops with "subscript out of bounds"
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 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.
Brian D. Ripley, ripley at 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