`stats::update()` weirdness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example 1: running at top level. This works. | |
model_str1 <- "speed~dist" | |
model1 <- lm(formula=model_str1, data=cars) | |
stats::update(model1, y = TRUE) | |
#> | |
#> Call: | |
#> lm(formula = model_str1, data = cars, y = TRUE) | |
#> | |
#> Coefficients: | |
#> (Intercept) dist | |
#> 8.2839 0.1656 | |
# Example 2: model is created in a function, then update() is called at the top | |
# level. This throws an error. | |
mymodel <- function() { | |
model_str <- "speed~dist" | |
model <- lm(formula=model_str, data=cars) | |
} | |
update(mymodel(), y = TRUE) | |
#> Error in stats::model.frame(formula = model_str, data = cars, drop.unused.levels = TRUE): object 'model_str' not found | |
# Example 3: Same as example 2, but there is a variable with the same name as | |
# one inside of mymodel(), and it causes incorrect result. | |
model_str <- "dist~speed" | |
update(mymodel(), y = TRUE) # <- This generates the wrong result!! | |
#> | |
#> Call: | |
#> lm(formula = model_str, data = cars, y = TRUE) | |
#> | |
#> Coefficients: | |
#> (Intercept) speed | |
#> -17.579 3.932 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment