R中的偏移规范 [英] Offset specification in R

查看:87
本文介绍了R中的偏移规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过阅读R中的glm的描述,我不清楚在公式中指定模型偏移量还是使用offset参数有什么区别.

在我的模型中,我有一个响应y,应将其除以偏移项w,为简单起见,假设我们有协变量x.我使用日志链接.

两者之间有什么区别

  glm(log(y)〜x + offset(-log(w))) 

  glm(log(y)〜x,offset = -log(w)) 

解决方案

两种方法是相同的.

这可以在文档中看到(粗体部分):

这可用于指定在拟合过程中要包含在线性预测器中的先验已知组件.该值应为NULL或长度等于事例数量的数字向量.一个或多个补偿项可以代替地也可以包括在公式中,如果指定了多个,则使用它们的总和.参见model.offset.

上面讨论了 glm 函数中的 offset 参数,并说它可以包含在公式中,也可以包含在中./p>

下面的一个简短示例表明上述说法是正确的:

数据

  y<-样本(1:2,50,rep = TRUE)x<-符文(50)w <-1:50df<-data.frame(y,x) 

第一个模型:

 >glm(log(y)〜x + offset(-log(w)))调用:glm(公式= log(y)〜x +偏移量(-log(w)))系数:(拦截)x3.6272 -0.4152自由度:49总分(即无效);48残留空偏差:44.52残余偏差:43.69 AIC:141.2 

第二种方式:

 >glm(log(y)〜x,offset = -log(w))呼叫:glm(公式= log(y)〜x,offset = -log(w))系数:(拦截)x3.6272 -0.4152自由度:49总分(即无效);48残留空偏差:44.52残余偏差:43.69 AIC:141.2 

如您所见,两者是相同的.

Reading the description of glm in R it is not clear to me what the difference is between specifying a model offset in the formula, or using the offset argument.

In my model I have a response y, that should be divided by an offset term w, and for simplicity lets assume we have the covariate x. I use log link.

What is the difference between

glm(log(y)~x+offset(-log(w)))

and

glm(log(y)~x,offset=-log(w))

解决方案

The two ways are identical.

This can be seen in the documentation (the bold part):

this can be used to specify an a priori known component to be included in the linear predictor during fitting. This should be NULL or a numeric vector of length equal to the number of cases. One or more offset terms can be included in the formula instead or as well, and if more than one is specified their sum is used. See model.offset.

The above talks about the offset argument in the glm function and says it can be included in the formula instead or as well.

A quick example below shows that the above is true:

Data

y <- sample(1:2, 50, rep=TRUE)
x <- runif(50)
w <- 1:50
df <- data.frame(y,x)

First model:

> glm(log(y)~x+offset(-log(w)))

Call:  glm(formula = log(y) ~ x + offset(-log(w)))

Coefficients:
(Intercept)            x  
     3.6272      -0.4152  

Degrees of Freedom: 49 Total (i.e. Null);  48 Residual
Null Deviance:      44.52 
Residual Deviance: 43.69    AIC: 141.2

And the second way:

> glm(log(y)~x,offset=-log(w))

Call:  glm(formula = log(y) ~ x, offset = -log(w))

Coefficients:
(Intercept)            x  
     3.6272      -0.4152  

Degrees of Freedom: 49 Total (i.e. Null);  48 Residual
Null Deviance:      44.52 
Residual Deviance: 43.69    AIC: 141.2

As you can see the two are identical.

这篇关于R中的偏移规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆