Exam1.1: Example1.1 from Generalized Linear Mixed Models: Modern...

Description Author(s) References See Also Examples

Description

Exam1.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

  2. Adeela Munawar (adeela.uaf@gmail.com)

References

  1. Stroup, W. W. (2012). Generalized Linear Mixed Models: Modern Concepts, Methods and Applications. CRC Press.

See Also

Table1.1

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#-------------------------------------------------------------
## Linear Model and results discussed in Article 1.2.1 after Table1.1
#-------------------------------------------------------------
data(Table1.1)
Exam1.1.lm1 <-
  lm(
      formula     =  y/Nx~x
    , data        =  Table1.1
    # , subset
    # , weights
    # , na.action
    , method      =  "qr"
    , model       =  TRUE
    , x           =  FALSE
    , y           =  FALSE
    , qr          =  TRUE
    , singular.ok =  TRUE
    , contrasts   =  NULL
    # , offset
    # , ...
  )
summary(Exam1.1.lm1 )

#-------------------------------------------------------------
## GLM fitting with logit link (family=binomial)
#-------------------------------------------------------------
Exam1.1.glm1 <-
  glm(
      formula     =  y/Nx~x
    , family      =  binomial(link = "logit")
    , data        =  Table1.1
    , weights     =  NULL
    # , subset
    # , na.action
    , start       =  NULL
    # , etastart
    # , mustart
    # , offset
    # , control     =  list(...)
    # , model       =  TRUE
    , method      =  "glm.fit"
    , x           =  FALSE
    , y           =  TRUE
    , contrasts   =  NULL
    # , ...
  )
## this glm() function gives warning message of non-integer success
summary(Exam1.1.glm1)

#-------------------------------------------------------------
## GLM fitting with logit link (family=Quasibinomial)
#-------------------------------------------------------------
Exam1.1.glm2 <-
  glm(
      formula   =  y/Nx~x
    , family    =  quasibinomial(link = "logit")
    , data      =  Table1.1
    , weights   =  NULL
    # , subset
    # , na.action
    , start     =  NULL
    # , etastart
    # , mustart
    # , offset
    # , control   =  list(...)
    # , model     =  TRUE
    , method    =  "glm.fit"
    , x         =  FALSE
    , y         =  TRUE
    , contrasts =  NULL
    # , ...
  )
## problem of "warning message of non-integer success" is overome by using quasibinomial family
summary(Exam1.1.glm2)

#-------------------------------------------------------------
## GLM fitting with survey package(produces same result as using quasi binomial family in glm)
#-------------------------------------------------------------
library(survey)
design   <-
  svydesign(
      ids          =  ~1
    , probs        =  NULL
    , strata       =  NULL
    , variables    =  NULL
    , fpc          =  NULL
    , data         =  Table1.1
    # , nest         =  FALSE
    # , check.strata =  !nest
    , weights      =  NULL
    , pps          =  FALSE
    # , ...
  )

Exam1.1.svyglm  <-
  svyglm(
      formula  =  y/Nx~x
    , design   =  design
    # , ...
    , family   =  quasibinomial(link="logit")
  )
# summary(Exam1.1.svyglm)

#-------------------------------------------------------------
## Figure 1.1
#-------------------------------------------------------------
Newdata     <-
  data.frame(
    Table1.1
    , LM       =  Exam1.1.lm1$fitted.values
    , GLM      =  Exam1.1.glm1$fitted.values
    , QB       =  Exam1.1.glm2$fitted.values
    , SM       =  Exam1.1.svyglm$fitted.values
  )
#-------------------------------------------------------------
## One Method to plot  Figure1.1
#-------------------------------------------------------------
library(ggplot2)

Figure1.1   <-
  ggplot(
      data     = Newdata
    , mapping  = aes(x=x,y=y/Nx)
  )     +
  geom_point (
    mapping  = aes(colour="black")
  )  +
  geom_point (
    data     = Newdata
    , mapping  = aes(x=x,y=LM,colour="blue"),shape=2
  )  +
  geom_line(
    data     = Newdata
    , mapping  = aes(x=x,y=LM,colour="blue")
  )   +
  geom_point (
    data     = Newdata
    , mapping  = aes(x=x,y=GLM,colour="red"),shape=3
  ) +
  geom_smooth (
    data     = Newdata
    , mapping  = aes(x=x,y=GLM,colour="red")
    , stat     = "smooth"
  ) +
  theme_bw()    +
  scale_colour_manual (
    values=c("black","blue","red"),
    labels=c("observed","LM","GLM")
  )  +
  guides (
    colour   = guide_legend(title="Plot")
  ) +
  labs (
    title     = "Linear Model vs Logistic Model"
  ) +
  labs (
    y         = "p"
  )
print(Figure1.1)

 #-------------------------------------------------------------
## Another way to plot Figure 1.1
#-------------------------------------------------------------
newdata   <-
  data.frame(
    P     =  c(
                Table1.1$y/Table1.1$Nx
              , Exam1.1.lm1$fitted.values
              , Exam1.1.glm1$fitted.values
               )
    , X     =  rep(Table1.1$x, 3)
    , group =  rep(c('Obs','LM','GLM'), each = length(Table1.1$x))
  )

Figure1.1      <-
  ggplot(
      data    = newdata
    , mapping = aes(x = X , y = P)
  )    +
  geom_point(
    mapping = aes(x = X , y = P, colour = group , shape=group)
  ) +
  geom_smooth(
    data    = subset(x = newdata, group == "LM")
    , mapping = aes(x=X,y=P)
    , col     = "green"
  ) +
  geom_smooth(
    data    = subset(x = newdata, group=="GLM")
    , mapping = aes(x = X , y = P)
    , col     = "red"
  ) +
  theme_bw() +
  labs(
    title   = "Linear Model vs Logistic Model"
  )
print(Figure1.1)

#-------------------------------------------------------------
## Correlation among p and fitted values using Gaussian link
#-------------------------------------------------------------
(lmCor <-
  cor(
    Table1.1$y/Table1.1$Nx,Exam1.1.lm1$fitted.values)
)
#-------------------------------------------------------------
## Correlation among p and fitted values using quasi binomial link
#-------------------------------------------------------------
(glmCor  <-
  cor(
    Table1.1$y/Table1.1$Nx,Exam1.1.glm1$fitted.values)
)

Example output

Call:
lm(formula = y/Nx ~ x, data = Table1.1, method = "qr", model = TRUE, 
    x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.18995 -0.09450  0.05671  0.08904  0.10883 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -0.08944    0.06625  -1.350     0.21    
x            0.11152    0.01120   9.958 3.71e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1175 on 9 degrees of freedom
Multiple R-squared:  0.9168,	Adjusted R-squared:  0.9075 
F-statistic: 99.16 on 1 and 9 DF,  p-value: 3.706e-06

Warning message:
In eval(family$initialize) : non-integer #successes in a binomial glm!

Call:
glm(formula = y/Nx ~ x, family = binomial(link = "logit"), data = Table1.1, 
    weights = NULL, start = NULL, method = "glm.fit", x = FALSE, 
    y = TRUE, contrasts = NULL)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-0.30433  -0.22562   0.04623   0.09882   0.44188  

Coefficients:
            Estimate Std. Error z value Pr(>|z|)  
(Intercept)  -3.9082     2.3234  -1.682   0.0925 .
x             0.7287     0.4057   1.796   0.0725 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 7.45149  on 10  degrees of freedom
Residual deviance: 0.67672  on  9  degrees of freedom
AIC: 8.3671

Number of Fisher Scoring iterations: 5


Call:
glm(formula = y/Nx ~ x, family = quasibinomial(link = "logit"), 
    data = Table1.1, weights = NULL, start = NULL, method = "glm.fit", 
    x = FALSE, y = TRUE, contrasts = NULL)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-0.30433  -0.22562   0.04623   0.09882   0.44188  

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -3.9082     0.6366  -6.139 0.000171 ***
x             0.7287     0.1112   6.555 0.000105 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for quasibinomial family taken to be 0.07508072)

    Null deviance: 7.45149  on 10  degrees of freedom
Residual deviance: 0.67672  on  9  degrees of freedom
AIC: NA

Number of Fisher Scoring iterations: 5

Loading required package: grid
Loading required package: Matrix
Loading required package: survival

Attaching package: 'survey'

The following object is masked from 'package:graphics':

    dotchart

`geom_smooth()` using method = 'loess' and formula 'y ~ x'
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
[1] 0.9574927
[1] 0.9810858

StroupGLMM documentation built on May 2, 2019, 9:42 a.m.