Using R for lack-of-fit F-test -
i learnt how use r perform f-test lack of fit of regression model, $h_0$: "there no lack of fit in regression model".
where df_1 degrees of freedom sslf (lack-of-fit sum of squares) , df_2 degrees of freedom sspe (sum of squares due pure error).
in r, f-test (say model 2 predictors) can calculated
anova(lm(y~x1+x2), lm(y~factor(x1)*factor(x2)))
example output:
model 1: y ~ x1 + x2 model 2: y ~ factor(x1) * factor(x2) res.df rss df sum of sq f pr(>f) 1 19 18.122 2 11 12.456 8 5.6658 0.6254 0.7419
f-statistic: 0.6254 p-value of 0.7419.
since p-value greater 0.05, not reject $h_0$ there no lack of fit. therefore model adequate.
what want know why use 2 models , why use command factor(x1)*factor(x2)
? apparently, 12.456 model 2
, magically sspe model 1
.
why?
you testing whether model interaction improves model fit.
model 1 corresponds additive effect of x1
, x2
.
one way "check" if complexity of model adequate (in case whether multiple regression additive effects make sense data) compare proposed model more flexible/complex model.
your model 2 has role of more flexible model. first predictors made categorical (by using factor(x1)
, factor(x2)
) , interaction between them constructed factor(x1)*factor(x2)
. interaction model includes additive model special case (i.e., model 1 nested in model 2) , has several parameters provide potentially better fit data.
you can see difference in number of parameters between 2 models in output anova
. model 2 has 8 parameters allow better fit because p-value non-significant conclude model 2 (with flexibility based on additional 8 parameters) not provide better fit data. thus, additive model provides decent enough fit data when compared model 2.
note trick above making categories (factors) of x1
, x2
works when number of unique values x1
, x2
low. if x1
, x2
numeric , each individual has own value model 2 not useful end same number of parameters hav observations. in situations more ad hoc modifications such binning variables used.
Comments
Post a Comment