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
The result with GLM model
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
And with 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
https://stackoverflow.com/questions/66937680/r-the-estimate-parameter-is-different-between-glm-model-and-optim-package April 04, 2021 at 11:05AM
没有评论:
发表评论