Logistic Regression in INLA

1,765 views
Skip to first unread message

Eddie

unread,
Jan 28, 2012, 2:33:40 PM1/28/12
to r-inla-disc...@googlegroups.com
Hi Guys,

I am trying to compare how differ is the logistic regression in GLM and INLA but get stuck on how I should I write the code. Really new in INLA! I need your help guys.

My code in glm is:
temperature.glm = glm(Response~Temperature, data=Temp,family=binomial)
confint(temperature.glm)
exp(temperature.glm$coefficients)
exp(confint(temperature.glm))


The data is something like this:

Response(Die/Not Die)    Temperature

            0                            27.00
            0                            27.05
            1                            30.00
            0                            28.90
            1                            31.22

I tried the 'SEEDS" example in Volume I,  but got an error message. Can somebody point me out how I should write it?

formula = Response ~ Temperature(model="iid")
mod.sst = inla(formula,data=inlasst,family="binomial",Ntrials=n)


Thanks in advance.

Cheers
Eddie

     

Finn Lindgren

unread,
Jan 28, 2012, 7:53:36 PM1/28/12
to r-inla-disc...@googlegroups.com
Your line

formula = Response ~ Temperature(model="iid")
should most likely be simply
formula = Response ~ Temperature
Alternatively
formula = Response ~ f(Temperature, model="iid")
but that would give you a pure random effects model which I don't think is what you want.

PS. It really helps if you state the precise error messages you get, so we don't have to guess...

/Finn

Eddie

unread,
Jan 31, 2012, 9:25:28 AM1/31/12
to r-inla-disc...@googlegroups.com
Hi Finn,

Thank you very much for your reply.

The INLA logistic regression is now running  using your suggestion : formula = Response ~ Temperature.

The error message I got was Error in eval(expr, envir, enclos) : could not find function "Temperature" due to my code formula = Response ~ Temperature(model="iid").

The formula = Response ~ f(Temperature, model="iid") also run smoothly. I tried to understand what is the concept behind "pure random effects".

Thanks again

Eddie



Finn Lindgren

unread,
Jan 31, 2012, 9:49:19 AM1/31/12
to r-inla-disc...@googlegroups.com
On 31 January 2012 14:25, Eddie <eddi...@gmail.com> wrote:
> The error message I got was Error in eval(expr, envir, enclos) : could not
> find function "Temperature" due to my code formula = Response ~
> Temperature(model="iid").

Good, that is the error message you should get, and R tries to
understand what you wanted to do. ;-)

> The formula = Response ~ f(Temperature, model="iid") also run smoothly. I
> tried to understand what is the concept behind "pure random effects".

That model says
y_i = a + e_i + eps_i, where both e_i and eps_i are independent
whereas the regression model is
y_i = a + t_i * beta + eps_i, where t_i are the temperatures.

Forget about the "pure random effects" model until you actually want a
model of that type... The regression models is what you want.

/Finn

Eddie

unread,
Feb 1, 2012, 1:30:35 PM2/1/12
to r-inla-disc...@googlegroups.com
Thanks Finn,

While I expected the logistic regression result would be almost similar between logistic regression in GLM and INLA, it somehow not.

GLM result :

Coefficients:

            Estimate Std. Error z value Pr(>|z|)   

(Intercept) -88.4505     8.6446  -10.23   <2e-16 ***

Temperature  2.9677     0.2893   10.26   <2e-16 ***

---

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


INLA result :

Fixed effects:
                         mean        sd        0.025quant    0.5quant  0.975quant
(Intercept) -28.8392982 3.0662949 -34.8361799 -28.8452536 -22.8074389
Temperature        0.7249629 0.1015447   0.5251831   0.7251992   0.9234421
                     kld
(Intercept)  1.768524e-07
Temperature  4.628354e-07


If I may summarised,

GLM : logit(p) = -88.4505 + 2.9677*Temperature

INLA: logit(p) = -28.8392982 + 0.7249629*Temperature


Just curious of the differences. :)


Eddie








INLA help

unread,
Feb 1, 2012, 2:23:26 PM2/1/12
to r-inla-disc...@googlegroups.com, Eddie
On Wed, 2012-02-01 at 10:30 -0800, Eddie wrote:
> Thanks Finn,
>
> While I expected the logistic regression result would be almost
> similar between logistic regression in GLM and INLA, it somehow not.
>

I think you just spesified the glm-model wrong using the weight-argument
instead of cbind(y,n)

> summary(inla(X.Response. ~ X.Temperature., data = d, family =
"binomial", Ntrials=d$X.n.))

Call:
c("inla(formula = X.Response. ~ X.Temperature., family = \"binomial\",
", " data = d, Ntrials = d$X.n.)")

Time used:
Pre-processing Running inla Post-processing Total
0.06314635277 0.04034376144 0.01054978371 0.11403989792

Fixed effects:
mean sd 0.025quant 0.5quant

(Intercept) -28.8241043091 3.0663123131 -34.8000865405 -28.8379472521
X.Temperature. 0.7244566083 0.1015451699 0.5239653221 0.7249486172
0.975quant kld
(Intercept) -22.7685405426 9.406954188e-06
X.Temperature. 0.9222016233 8.001251409e-06

The model has no random effects

The model has no hyperparameters

Expected number of effective parameters(std dev): 2.007(0.00)
Number of equivalent replicates : 338.33

Marginal Likelihood: 0.00
Warning: Interpret the marginal likelihood with care if the prior model
is improper.


> summary(glm(cbind(X.Response., X.n.) ~ X.Temperature., data = d,
family = binomial()))

Call:
glm(formula = cbind(X.Response., X.n.) ~ X.Temperature., family =
binomial(),
data = d)

Deviance Residuals:
Min 1Q Median 3Q Max
-1.75038246 -0.92247152 0.06408632 0.53389689 0.89910308

Coefficients:
Estimate Std. Error z value Pr(>|z|)

(Intercept) -28.8192031 3.0665683 -9.39787 < 2.22e-16 ***
X.Temperature. 0.7242266 0.1015535 7.13148 9.9294e-13 ***


---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 407.38952 on 678 degrees of freedom
Residual deviance: 359.29633 on 677 degrees of freedom
AIC: 1194.6843

Number of Fisher Scoring iterations: 5

--
INLA help <he...@r-inla.org>
R-INLA

ResponseTemp.csv

INLA help

unread,
Feb 1, 2012, 2:31:40 PM2/1/12
to r-inla-disc...@googlegroups.com, Eddie
sorry, its

glm( cbind(y, n - y) ~ ....)

so it should read

glm(cbind(X.Response., X.n. - X.Response.) ~ X.Temperature., ....

Eddie

unread,
Feb 2, 2012, 3:28:26 PM2/2/12
to r-inla-disc...@googlegroups.com, Eddie, he...@r-inla.prg
Hi all,

Finally, I managed to compare the result between logistic regression in glm and INLA.  I was too much influenced by the n value in the inla example. :) which is actually not needed in my case!

Thanks to inla team for being so helpful!

Here is the coefficients comparison :

                       GLM            INLA
(Intercept)     -88.4505       -88.585533
Temperature   2.9677        2.972168

Call:
glm(formula = Response ~ Temperature, family = binomial, data = sst)


Deviance Residuals:
    Min       1Q   Median       3Q      Max 
-3.0615  -0.9446   0.3679   0.9651   1.8032 

Coefficients:
            Estimate Std. Error z value Pr(>|z|)   
(Intercept) -88.4505     8.6446  -10.23   <2e-16 ***
Temperature   2.9677     0.2893   10.26   <2e-16 ***

---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 906.52  on 678  degrees of freedom
Residual deviance: 745.56  on 677  degrees of freedom
AIC: 749.56


Number of Fisher Scoring iterations: 5

Call:
"inla(formula = Response ~ Temperature, family = \"binomial\", data = sst)"


Time used:
 Pre-processing    Running inla Post-processing           Total
      0.8160000       0.4180000       0.1790001       1.4130001

Fixed effects:
                  mean        sd  0.025quant   0.5quant 0.975quant          kld
(Intercept) -88.585533 8.6440735 -106.075380 -88.404891 -72.119733 0.0001272542
Temperature   2.972168 0.2892398    2.421648   2.966076   3.557313 0.0001223206


The model has no random effects

The model has no hyperparameters

Expected number of effective parameters(std dev): 2.002(0.00)
Number of equivalent replicates : 339.15
Reply all
Reply to author
Forward
0 new messages