Commit b064860f authored by Rubin, Paul's avatar Rubin, Paul

Modified the kludge for models specified as formulas, to handle those created inside functions.

parent 0572a75f
......@@ -53,15 +53,18 @@ stepwise <-
# Acquire the full and initial models as formulas. If they are
# entered as formulas, convert them to get their environments
# squared away.
# Note: "showEnv = F" is necessary to avoid having an
# environment identifier break things if the model is
# defined inside a function.
if (is.character(full.model)) {
fm <- as.formula(full.model)
} else {
fm <- as.formula(capture.output(print(full.model)))
fm <- as.formula(capture.output(print(full.model, showEnv = F)))
}
if (is.character(initial.model)) {
im <- as.formula(initial.model)
} else {
im <- as.formula(capture.output(print(initial.model)))
im <- as.formula(capture.output(print(initial.model, showEnv = F)))
}
# Fit the full model.
full <- lm(fm, data);
......
This diff is collapsed.
......@@ -36,15 +36,18 @@ stepwise <-
# Acquire the full and initial models as formulas. If they are
# entered as formulas, convert them to get their environments
# squared away.
# Note: "showEnv = F" is necessary to avoid having an
# environment identifier break things if the model is
# defined inside a function.
if (is.character(full.model)) {
fm <- as.formula(full.model)
} else {
fm <- as.formula(capture.output(print(full.model)))
fm <- as.formula(capture.output(print(full.model, showEnv = F)))
}
if (is.character(initial.model)) {
im <- as.formula(initial.model)
} else {
im <- as.formula(capture.output(print(initial.model)))
im <- as.formula(capture.output(print(initial.model, showEnv = F)))
}
# Fit the full model.
full <- lm(fm, data);
......@@ -314,3 +317,16 @@ junk <- iris[c(1, 150), ]
stepwise(Sepal.Width ~ Sepal.Length + Petal.Length + Petal.Width, Sepal.Width ~ 1, aToEnter, aToLeave, data = junk)
```
# Calling from inside a function
Verify that stepwise can be called from inside a function (rather than from the global environment), including building the models inside the function.
```{r}
caller <- function() {
fm <- as.formula("Sepal.Width ~ Sepal.Length + Petal.Width + Petal.Length")
im <- Sepal.Width ~ 1
stepwise(fm, im, aToEnter, aToLeave, data = iris)
}
caller()
```
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment