问题
I am not sure how to use the add1 command. Suppose I have a model
y=b0+b1*x1
and I would like to know if it would be a better fit to add more independent variables. Now I would test all models
y=b0+b1*x1+b2*x2
with different x2 (my different independent variables). The add1 command somehow needs a "scope". I am not sure of, what that is. I could not find out how to use the add1 command. If I do this:
add1(fittedmodel)
I get an error, so I suppose I need to specify which variable I want to use by hand. That is fine, that's actually what I wanted but wasn't sure if it is like that. If I do
add1(fittedmodel, scope=x1+x2, test="F")
inserting a specific variable for x2, I get the following output:
Single term additions
Model:
sl ~ le
Df Sum of Sq RSS AIC F value Pr(>F)
<none> 0.51211 -523.44
ky 1 0.00097796 0.51113 -521.63 0.1856 0.6676
and I am not sure of if this is what I want. The Model it describes sl~le
is not what I wanted (sl~le+ky
), but that may just be the model it starts of with?
Then I do not know what the <none>
means.
Would this now mean that the F-Test-value for comparing model sl~le
to model sl~le
is 0.1856? Or do I interpret the output wrong?
Then, even if this is right, how do I do it for a model 'sl~le+ky+le:ky', that is if I do have an interaction? I don't seem to understand the scope parameter in the add1() command, but I need it, because without it, add1() does not work!
回答1:
Here is an example of how to use the scope and interpret the results of add1
.
All of this could have been easily found by reading ?add1
and looking at the examples.
# create some data
set.seed(1)
DF <- data.frame(x1 = sample(letters[1:2], 50, replace = TRUE), x2 = sample(letters[3:4],
50, replace = TRUE))
library(plyr)
DF <- ddply(DF, .(x1, x2), mutate, y = sample(1:10, 1))
DF <- ddply(DF, .(x1, x2), mutate, y = y + rnorm(length(y), 0, 2))
# a simple model with just y~x1
simple <- lm(y ~ x1, data = DF)
# add a single term
add1(simple, scope = ~x1 + x2, test = "F")
## Single term additions
##
## Model:
## y ~ x1
## Df Sum of Sq RSS AIC F value Pr(>F)
## <none> 565 125
## x2 1 93.9 471 118 9.37 0.0036 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The tables goes through all possibilities of adding 1 variable given the scope and as the help (?add1
) states under Value
Value
An object of class "anova" summarizing the differences in fit between the models.
So this states that adding x2
the model contain x1
will give a lower AIC, and the F
test test the difference between models.
If you want to test whether adding the interaction improves the model then you would need to fit the main effects model first, then use the scope ~x1*x2
which expands out to ~x1+x2+x1:x2
simple_2 <- lm(y ~ x1 + x2, data = DF)
add1(simple_2, scope = ~x1 * x2, test = "F")
## Single term additions
##
## Model:
## y ~ x1 + x2
## Df Sum of Sq RSS AIC F value Pr(>F)
## <none> 471 118.2
## x1:x2 1 289 182 72.6 73 4.7e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
回答2:
In which case you could have used drop1()
function. drop1(fittedmodel)
is used when we do backward selection. It starts from full model, and returns p-value for each case when one predictor is dropped. So if you have only 2 predictors to compare, drop1()
function would have done a better job.
来源:https://stackoverflow.com/questions/12923577/scope-from-add1-command-in-r