如何获得 R 中具有偏移量的对比度的估计值和置信区间
How to get an estimate and confidence interval for a contrast in R with offset
我在 R 中安装了泊松 GLM 模型,看起来像这样:
glm(Outcome~Exposure + Var1 + offset(log(persontime)),family=poisson,data=G))
其中 Outcome 最终将是一个比率,Exposure 是一个连续变量,Var1 是一个具有三个水平的因素。
从输出结果来看很简单:
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -5.6998 0.1963 -29.029 < 2e-16
Exposure 4.7482 1.0793 4.399 1.09e-05
Var1Thing1 -0.2930 0.2008 -1.459 0.144524
Var1Thin 1.0395 0.2037 5.103 3.34e-07
Var1Thing3 0.7722 0.2201 3.508 0.000451
获得曝光增加一个单位的估计值。但一个单位的增加实际上并不是特别有意义。实际上增加 0.025 的可能性更大。估计 that 也不是特别困难,但我想要一个置信区间和估计值。我的直觉是我需要使用 contrast 包,但是下面产生了一个错误:
diff <- contrast(Fit,list(Exposure=0.030,Var1="Thing1"),list(Exposure=0.005,Type="Thing1"))
"Error in offset(log(persontime)) : object 'persontime' not found"
知道我做错了什么吗?
您想使用 confint
函数(在本例中将调用 MASS:::confint.glm
方法),如:
confint(Fit)
由于标准误差是模型比例与模型中变量比例的线性变化 'Exposure' 成线性关系,您可以简单地将置信区间乘以比例差异来获得置信度较小的 'unit' 变化。
愚蠢的例子:
假设您想检验这样的假设:人们在喝了更多酒后更容易倒下。您通过随机为个人提供不同量的酒精(以毫升为单位)并计算每个人倒下的次数来对此进行测试。您的型号是:
Fit <- glm(falls ~ alcohol_ml,data=myData, family=poisson)
系数 table 是
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -5.6998 0.1963 -29.029 < 2e-16
Alcohol_ml 4.7482 1.0793 4.399 1.09e-05
并且酒精的置信区间为 4-6(保持简单)。现在一位同事要求您给出以盎司为单位的置信区间。您所要做的就是按换算系数(每毫升 29.5735 盎司)的置信区间进行缩放,如:
c(4,6) * 29.5735 # effect per ounce alcohol [notice multiplication is used to rescale here]
或者,您可以重新缩放数据并重新拟合模型:
mydata$alcohol_oz <- mydata$alcohol_ml / 29.5735 #[notice division is used to rescale here]
Fit <- glm(falls ~ alcohol_oz,data=myData, family=poisson)
或者您可以直接在模型中重新缩放数据:
#[again notice that division is used here]
Fit <- glm(falls ~ I(alcohol_ml/29.5735),data=myData, family=poisson)
无论哪种方式,您都将在新量表上获得相同的置信区间。
回到你的例子:如果你的 Exposure
单位太大以至于你不太可能观察到个体内部的这种变化并且更小的变化更容易解释,只需重新缩放你的变量 'Exposure'(如 myData$Exposure_newScale = myData$Exposure / 0.030
,因此 Exposure_newScale
是 0.030
的倍数)或使用这些方法之一重新调整置信区间。
我在 R 中安装了泊松 GLM 模型,看起来像这样:
glm(Outcome~Exposure + Var1 + offset(log(persontime)),family=poisson,data=G))
其中 Outcome 最终将是一个比率,Exposure 是一个连续变量,Var1 是一个具有三个水平的因素。
从输出结果来看很简单:
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -5.6998 0.1963 -29.029 < 2e-16
Exposure 4.7482 1.0793 4.399 1.09e-05
Var1Thing1 -0.2930 0.2008 -1.459 0.144524
Var1Thin 1.0395 0.2037 5.103 3.34e-07
Var1Thing3 0.7722 0.2201 3.508 0.000451
获得曝光增加一个单位的估计值。但一个单位的增加实际上并不是特别有意义。实际上增加 0.025 的可能性更大。估计 that 也不是特别困难,但我想要一个置信区间和估计值。我的直觉是我需要使用 contrast 包,但是下面产生了一个错误:
diff <- contrast(Fit,list(Exposure=0.030,Var1="Thing1"),list(Exposure=0.005,Type="Thing1"))
"Error in offset(log(persontime)) : object 'persontime' not found"
知道我做错了什么吗?
您想使用 confint
函数(在本例中将调用 MASS:::confint.glm
方法),如:
confint(Fit)
由于标准误差是模型比例与模型中变量比例的线性变化 'Exposure' 成线性关系,您可以简单地将置信区间乘以比例差异来获得置信度较小的 'unit' 变化。
愚蠢的例子:
假设您想检验这样的假设:人们在喝了更多酒后更容易倒下。您通过随机为个人提供不同量的酒精(以毫升为单位)并计算每个人倒下的次数来对此进行测试。您的型号是:
Fit <- glm(falls ~ alcohol_ml,data=myData, family=poisson)
系数 table 是
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -5.6998 0.1963 -29.029 < 2e-16
Alcohol_ml 4.7482 1.0793 4.399 1.09e-05
并且酒精的置信区间为 4-6(保持简单)。现在一位同事要求您给出以盎司为单位的置信区间。您所要做的就是按换算系数(每毫升 29.5735 盎司)的置信区间进行缩放,如:
c(4,6) * 29.5735 # effect per ounce alcohol [notice multiplication is used to rescale here]
或者,您可以重新缩放数据并重新拟合模型:
mydata$alcohol_oz <- mydata$alcohol_ml / 29.5735 #[notice division is used to rescale here]
Fit <- glm(falls ~ alcohol_oz,data=myData, family=poisson)
或者您可以直接在模型中重新缩放数据:
#[again notice that division is used here]
Fit <- glm(falls ~ I(alcohol_ml/29.5735),data=myData, family=poisson)
无论哪种方式,您都将在新量表上获得相同的置信区间。
回到你的例子:如果你的 Exposure
单位太大以至于你不太可能观察到个体内部的这种变化并且更小的变化更容易解释,只需重新缩放你的变量 'Exposure'(如 myData$Exposure_newScale = myData$Exposure / 0.030
,因此 Exposure_newScale
是 0.030
的倍数)或使用这些方法之一重新调整置信区间。