【发布时间】:2013-07-02 10:53:23
【问题描述】:
使用这个数据集:
并以这个问题为基础:
How to fit predefined offsets to models containing categorical variables in R
为了在另一个测试数据集上测试模型的有效性,我想从以下位置获取拟合模型:
ModelA<-lm(Response1~Categorical)
并将其拟合到关系 B:
Response2~Categorical
每种情况下的响应变量都是相同的。
上面的链接提供了一个解决方案,说明如何为分类变量的级别拟合偏移量,这对于我的数据将涉及:
# compute the offsets for each level of Categorical from the following model:
m<-lm(Response1~Categorical,data=dat)
summary(m)
#Create vector of offsets for variable
o <- with(dat, ifelse(Categorical == "Y", 0.25773, -0.25773))
#run second model with offsets from first model
m1<-lm(dat$Response2 ~ 1 + offset(o))
但是,当我通过将这些已知偏移量指定给关系来检查这是否有效,然后使用没有指定偏移量的相同模型进行检查时,因此:
# run model using Response1 to get values for slope offsets
m<-lm(Response1 ~ Categorical,data=dat)
summary(m)
# Specify offsets from this in the model of the same data (i.e. still using Response1)
o <- with(dat, ifelse(Categorical == "Y", 0.25773, -0.25773))
m1<-lm(dat$Response1 ~ 1 + offset(o))
#check the residuals from m and m2 are identical
m$residuals
m2$residuals
残差不一样,说明方法不行。
我想知道:
1) 有没有人知道如何为分类变量的级别指定偏移量? 2) 除了级别的偏移量之外,您能否建议如何指定和偏移此类变量的截距项?
后者对于连续变量来说足够简单,因为只有一个截距:
# run model using Response1 to get values for intercept and slope offsets
m<-lm(Response1~log(Continuous),data=dat)
summary(m)
# Specify offsets for the intercept and slope for the model involving the second response variable
m <- lm(Response2 ~ 0+offset(rep(0.22483, nrow(dat))) + offset( -0.07115*log(Continuous)))
但我不清楚这将如何转移到分类变量。
非常感谢。
【问题讨论】:
-
挠头。你能解释一下为什么你认为如果模型改变了,残差应该是一样的吗? (如果他们没有不同,我会感到惊讶。)
-
嗨 DWin,我可能没有很好地解释 v,抱歉。如果模型发生变化,残差确实会有所不同 - 我的最终目标是在不同的响应之间进行比较,所以我期望残差发生变化。但是,因为我不确定向我建议的偏移方法是否真的有效,所以我想检查一下。为此,我安装了一个没有指定偏移量的模型,然后安装了相同的模型,但 with 指定了偏移量(从第一个模型中提升),即我正在拟合 R woudl 无论如何都适合的偏移量。如果我的偏移编码是正确的,那么在这个“检查”中 2 组残差将是相同的。
-
不幸的是,当使用表明我的偏移方法错误的分类变量时,它们并不相同
标签: r regression linear-regression categorical-data