library(MASS)
Boston$old <- as.numeric(Boston$age >= 95)
# realizar la regresión y asígnar el resultado a mod_bc.
# extraer los coeficientes estimados del modelo y asignar el resultado a params.
# graficar medv contra indus y agregar las líneas de regresión.
# realizar la regresión y asígnar el resultado a mod_bc.
mod_bc <- lm(medv ~ indus*old, data = Boston)
# extraer los coeficientes estimados del modelo y asignar el resultado a params.
params <- coef(mod_bc)
# graficar medv contra indus y agregar las líneas de regresión.
plot(medv ~ indus, data = Boston)
abline(a = params[1], b = params[2], col = "red")
abline(a = params[1] + params[3], b = params[2] + params[4], col = "darkblue")
test_or({
test_object("mod_bc")
},{
f <- ex() %>% override_solution("lm(medv ~ old*indus, data = Boston)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("lm(Boston$medv ~ Boston$indus*Boston$old)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("lm(Boston$medv ~ Boston$old*Boston$indus)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("attach(Boston);lm(medv ~ indus*old)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("attach(Boston);lm(medv ~ old*indus)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("lm(medv ~ indus + old + indus:old, data = Boston)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("lm(medv ~ old + indus + old:indus, data = Boston)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("lm(Boston$medv ~ Boston$indus + Boston$old + Boston$indus:Boston$old)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("lm(Boston$medv ~ Boston$old + Boston$indus + Boston$old:Boston$indus)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("attach(Boston);lm(medv ~ indus + old + indus:old)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
},{
f <- ex() %>% override_solution("attach(Boston);lm(medv ~ old + indus + old:indus)") %>% check_function("lm")
f %>% check_arg("formula") %>% check_equal()
})
ex() %>% check_object("params") %>% check_equal()
test_or({
ex() %>% check_function("plot") %>% {
check_arg(., "formula") %>% check_equal()
check_arg(., "data") %>% check_equal()
}},{
f <- ex() %>% override_solution("plot(Boston$indus, Boston$medv)") %>% check_function("plot")
f %>% {
check_arg(., "x") %>% check_equal()
check_arg(., "y") %>% check_equal()
}},{
f <- ex() %>% override_solution("plot(Boston$medv ~ Boston$indus)") %>% check_function("plot")
f %>% check_arg("formula") %>% check_equal()
})
test_or({
ex() %>% check_function("abline", index = 1) %>% {
check_arg(., "a") %>% check_equal()
check_arg(., "b") %>% check_equal()
}
ex() %>% check_function("abline", index = 2) %>% {
check_arg(., "a") %>% check_equal()
check_arg(., "b") %>% check_equal()
}},{
f <- ex() %>% override_solution("abline(a = params[1], b = params[2]);abline(coef = c(params[1] + params[3], params[2] + params[4]))")
f %>% check_function("abline", index = 1) %>% {
check_arg(., "a") %>% check_equal()
check_arg(., "b") %>% check_equal()
}
f %>% check_function("abline", index = 2) %>% check_arg("coef") %>% check_equal()
},{
f <- ex() %>% override_solution("abline(coef = c(params[1], params[2]));abline(coef = c(params[1] + params[3], params[2] + params[4]))")
f %>% check_function("abline", index = 1) %>% check_arg("coef") %>% check_equal()
f %>% check_function("abline", index = 2) %>% check_arg("coef") %>% check_equal()
},{
f <- ex() %>% override_solution("abline(coef = c(params[1], params[2]));abline(a = params[1] + params[3], b = params[2] + params[4])")
f %>% check_function("abline", index = 1) %>% check_arg("coef") %>% check_equal()
f %>% check_function("abline", index = 2) %>% {
check_arg(., "a") %>% check_equal()
check_arg(., "b") %>% check_equal()
}})
success_msg("¡Correcto! Se puede ver que la disminución en el valor medio de la vivienda debido a un aumento en la proporción de edificios industriales es, en términos absolutos, menor en los suburbios donde la proporción de edificios antiguos es alta.")