質問

I'm using step() to find a model to adjust a score based on other variables. My full model is thus :

mod<-lm(Adjusted.score ~ original.score + X1 + X2 + X3 + ... + X10)

It's logical that I need to keep the variable original.score in the final model but step() always omits it. Is there a way to force step() to keep the variable original.score and find the best combinations of the other variables to accompany it?

役に立ちましたか?

解決

Are you perhaps looking for this (from ?step):

The set of models searched is determined by the ‘scope’ argument.
The right-hand-side of its ‘lower’ component is always included in
the model, and right-hand-side of the model is included in the
‘upper’ component.  If ‘scope’ is a single formula, it specifies
the ‘upper’ component, and the ‘lower’ model is empty.  If ‘scope’
is missing, the initial model is used as the ‘upper’ model.

Models specified by ‘scope’ can be templates to update ‘object’ as
used by ‘update.formula’.  So using ‘.’ in a ‘scope’ formula means
‘what is already there’, with ‘.^2’ indicating all interactions of
existing terms.

Responding to your comment: I haven't done this sort of thing before but a small example included below seems to do what you are asking for:

x = setNames(lapply(1:5, function (x) runif(100)), letters[1:5])
step(lmObj, scope=list(lower=as.formula(a ~ b), upper=as.formula(a ~ .)))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top