强制步骤()保持一定的价值

时间:2014-03-22 16:28:17

标签: r linear-regression

我正在使用step()查找模型以根据其他变量调整分数。因此,我的完整模型是:

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

我需要将变量original.score保留在最终模型中,但step()总是忽略它,这是合乎逻辑的。有没有办法强制step()保留变量original.score并找到伴随它的其他变量的最佳组合?

1 个答案:

答案 0 :(得分:4)

你是否正在寻找这个(来自?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.

回应你的评论:我之前没有做过这种事情,但下面的一个小例子似乎可以满足你的要求:

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 ~ .)))