R-回归 - 将因子水平设置为预定义值



如何在回归模型中将因子级别设置为预定义值?

使用lm指定回归模型时,我似乎无法在因子变量上使用offset函数。例如,设置约束 Sepal.Width = 0.5其中 Sepal.Width是连续变量,我将代码

lm(Sepal.Length ~ as.factor(Species) + offset(Sepal.Width*0.5), data = iris)

我该如何设置Species的第一级(称为setosa)等于预定义值五个?IE。如何将上述回归模型限制为setosa = 5

计算模型矩阵并以此为单位:

mm <- model.matrix(Sepal.Length ~ Species - 1, iris)
fm <- lm(Sepal.Length ~ mm[, -1] + offset(.5 * mm[, 1]) - 1, iris)

修订

最新更新