R:GLM模型和optim()包之间的估算参数不同 [英] R: The estimate parameter is different between GLM model and optim() package

查看:72
本文介绍了R:GLM模型和optim()包之间的估算参数不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用R中的optim()包找到估算参数.然后将我的结果与R中的GLM模型进行比较.代码是

I want to find estimate parameter with optim() package in R. And I compare my result with GLM model in R. The code is

d <- read.delim("http://dnett.github.io/S510/Disease.txt")
d$disease=factor(d$disease)
d$ses=factor(d$ses)
d$sector=factor(d$sector)
str(d)

oreduced <- glm(disease~age+sector, family=binomial(link=logit), data=d)
summary(oreduced)

y<-as.numeric(as.character(d$disease))
x1<-as.numeric(as.character(d$age))
x2<-as.numeric(as.character(d$sector))

nlldbin=function(param){
  eta<-param[1]+param[2]*x1+param[3]*x2
  p<-1/(1+exp(-eta))
  -sum(y*log(p)+(1-y)*log(1-p),na.rm=TRUE)
}
MLE_estimates<-optim(c(Intercept=0.1,age=0.1,sector2=0.1),nlldbin,hessian=TRUE)

MLE_estimatesenter

使用GLM模型的结果

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept) -2.15966    0.34388  -6.280 3.38e-10 ***
age          0.02681    0.00865   3.100 0.001936 ** 
sector2      1.18169    0.33696   3.507 0.000453 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

并带有optim()

$par
  Intercept         age     sector2 
-3.34005918  0.02680405  1.18101449 

有人可以告诉我其原因为何以及如何解决吗?谢谢

Can someone please tell me why its different and how to fix this? Thank you

推荐答案

您已经给R带来了两个不同的问题.在您的GLM中,公式中的所有参数都是因子变量.这意味着您已经告诉R他们只能接受特定值(例如 d $ disease 只能接受值0和1).在您的MLE方法中,您已经将它们转换为数字变量,这意味着它们可以采用任何值,并且您的数据恰好使用了一小组值.

You've given R two different problems. In your GLM, all of the parameters in the formula are factor variables. This mean that you've told R that they can only take particular values (e.g. d$disease can only take values 0 and 1). In your MLE approach, you've converted them to numeric variables, meaning that they can take any value and that your data just happens to use a small set of values.

修复"是仅给R一个要解决的问题.例如,如果改用不使用因子变量的 glm(y〜x1 + x2,family = binomial(link = logit)),则两个MLE的参数估计值几乎相同与拟合的模型一样.您之前已经看过.

The "fix" is to only give R one problem to solve. For example, if you instead fit glm(y~x1+x2, family=binomial(link=logit)), which uses no factor variables, you get pretty much the same parameter estimates with both the MLE as with the fitted model. You've seen this before.

这篇关于R:GLM模型和optim()包之间的估算参数不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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