Commit 6ca3f0fc authored by Rubin, Paul's avatar Rubin, Paul

Fixed to work with "." in models.

parent ff3287b9
......@@ -55,10 +55,6 @@ stepwise <-
warning("Your alpha-to-enter is greater than your alpha-to-leave, which could throw the function into an infinite loop.\n")
return(NA)
}
# Deal with a missing data argument.
if (is.null(data)) {
data <- parent.frame()
}
# Warning: horrible kludge coming!
# Acquire the full and initial models as formulas. If they are
# entered as formulas, convert them to get their environments
......@@ -76,6 +72,17 @@ stepwise <-
} else {
im <- as.formula(capture.output(print(initial.model, showEnv = F)))
}
# Deal with a missing data argument.
if (is.null(data)) {
# Catch the use of "." in a formula when the data argument is null.
if ("." %in% all.vars(fm) | "." %in% all.vars(im)) {
warning("In order to use the shortcut '.' in a formula, you must explicitly specify the data source via the 'data' argument.\n")
return(NA)
} else {
# Use the parent environment.
data <- parent.frame()
}
}
# Fit the full model.
full <- lm(fm, data);
# Sanity check: do not allow an overspecified full model.
......@@ -262,6 +269,18 @@ The result of this is a missing model.
result
```
The "." shortcut can be used to specify that the right-hand side of a model contain all terms except the dependent variable. *It requires that the data source be specified explicitly.* So the following works.
```{r}
stepwise(Fertility ~ ., Fertility ~ 1, aToEnter, aToLeave, data = swiss)
```
If we omit the `data` argument and rely on the `swiss` data set being attached, we get an error.
```{r}
stepwise(Fertility ~ ., Fertility ~ 1, aToEnter, aToLeave)
```
We are done with the `swiss` data set.
```{r}
......
This diff is collapsed.
......@@ -28,10 +28,6 @@ stepwise <-
warning("Your alpha-to-enter is greater than your alpha-to-leave, which could throw the function into an infinite loop.\n")
return(NA)
}
# Deal with a missing data argument.
if (is.null(data)) {
data <- parent.frame()
}
# Warning: horrible kludge coming!
# Acquire the full and initial models as formulas. If they are
# entered as formulas, convert them to get their environments
......@@ -49,6 +45,17 @@ stepwise <-
} else {
im <- as.formula(capture.output(print(initial.model, showEnv = F)))
}
# Deal with a missing data argument.
if (is.null(data)) {
# Catch the use of "." in a formula when the data argument is null.
if ("." %in% all.vars(fm) | "." %in% all.vars(im)) {
warning("In order to use the shortcut '.' in a formula, you must explicitly specify the data source via the 'data' argument.\n")
return(NA)
} else {
# Use the parent environment.
data <- parent.frame()
}
}
# Fit the full model.
full <- lm(fm, data);
# Sanity check: do not allow an overspecified full model.
......@@ -356,3 +363,21 @@ caller <- function() {
caller()
```
# "." shortcut
Verify that the "." shortcut works in a formula so long as the data source is explicitly specified.
```{r}
stepwise(Fertility ~ ., Fertility ~ 1, 0.05, 0.1, data = swiss)
```
```{r}
stepwise(Fertility ~ 1, Fertility ~ ., 0.05, 0.1, data = swiss)
```
Check the error message if the data argument is missing when "." is used.
```{r}
stepwise(Fertility ~ ., Fertility ~ 1, 0.05, 0.1)
```
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