-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkovLinearModelExamples.R
334 lines (247 loc) · 7.83 KB
/
MarkovLinearModelExamples.R
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
source("MarkovLinearModelExamples.R")
##############################
#
# This file contains an implementation of the examples given in
# "Estimations of means and variances in a Markov linear model"
# by A. Gutierrez and S. Mueller
#
##############################
##############################
#### Example 11
##############################
set.seed(42)
number_col <- 2 # number of columns
number_lines <- numeric(number_col)
number_lines <- c(2, 2) # number of machines in each column
### Definition of the transition kernels
Q <- list(number_col)
Q[[1]] <- c(1/2, 1/2)
Q[[2]] <- matrix(c(3/4, 1/4, 1/4, 3/4), nrow=2, byrow=TRUE)
### Definition of the quality matrix
V <- matrix( c(2,1,1,1), nrow=2, byrow = TRUE) # matrix of variances
ES <- matrix( c(0,1,-2,2), nrow=2, byrow = TRUE) # matrix of means
### Sampling
observation <- sample_path_quality(number_col, number_lines, Q, ES,V)
n <- 1000
for (i in 2:n){
observation<-bind_rows(observation, sample_path_quality(number_col, number_lines, Q, ES,V))
}
Q_estimator(observation, number_col, number_lines) -> Q_est
T_U(observation, number_col, number_lines) -> TU
round(apply(TU, 2, function(x) x[1]-x[-1]),2)
T_QU(observation, number_col, number_lines, Q) -> TQU
round(apply(TQU, 2, function(x) x[1]-x[-1]),2)
T_QU(observation, number_col, number_lines, Q_est) -> TQU_est
round(apply(TQU_est, 2, function(x) x[1]-x[-1]),2)
S_U(observation, number_col, number_lines)->SU
round(apply(SU, 2, function(x) x[1]-x[-1]),2)
S_QU(observation, number_col, number_lines, Q)->SQU
round(apply(SQU, 2, function(x) x[1]-x[-1]),2)
S_QU(observation, number_col, number_lines, Q_est)->SQUest
round(apply(SQUest, 2, function(x) x[1]-x[-1]),2)
##############################
#### Example 13 - Tooth growth
##############################
data(ToothGrowth)
str(ToothGrowth)
# covariates <- supp and dose as factor
# response <- length
ToothGrowth -> data
data <- data[c(2, 3, 1)]
data$supp <-
factor(data$supp,
levels = c("VC", "OJ"),
labels = c("VC", "OJ"))
data$dose <-
factor(
data$dose,
levels = c(0.5, 1.0, 2.0),
labels = c("Dose 0.5", "Dose 1.0", "Dose 2.0")
)
str(data)
qplot(
supp,
len,
data = data,
facets = ~ dose,
main = "Tooth growth of guinea pigs",
xlab = "Supplement type",
ylab = "Tooth length"
) +
geom_violin(aes(fill = supp)) +
geom_jitter(shape = 16, position = position_jitter(0.1)) +
theme(legend.position = "none")
### Classical linear regression with factors
fit <- aov(len ~ supp + dose, data = data)
summary(fit)
fit$coefficients
### Markov linear regression
number_col <- 2 # number of columns
number_lines <- numeric(number_col) # number of lines in each column
number_lines <- c(2,3) # first colum supp, second dose
n <- nrow(data)
observation <- data
observation$supp <- as.numeric(observation$supp)
observation$dose <- as.numeric(observation$dose)
names(observation)[number_col+1] <- "b"
T_U(observation, number_col, number_lines)-> TU
apply(TU, 2, function(x) x[-1]-x[1])
fit$coefficients
# we obtain the same results as with the linear regression
# we can check if the experiment is balanced
Q_estimator(observation, number_col, number_lines)
T_QU(observation, number_col, number_lines, Q_estimator(observation, number_col, number_lines)) -> TQU
TQU -TU
# We obtain the same results, since balanced experiment and no bias.
######## Add variance
S_U(observation, number_col, number_lines)-> SU
SU
apply(SU, 2, function(x) x[1]-x[-1])
S_QU(observation, number_col, number_lines, Q_estimator(observation, number_col, number_lines))-> SQU
SQU
apply(SU, 2, function(x) x[1]-x[-1])
data %>% group_by(supp) %>%
summarize(variance =var(len)) -> res
(res$variance[1]-res$variance[2]) *29/30
data %>% group_by(dose) %>%
summarize(variance =var(len)) -> res
(res$variance[1]-res$variance[2]) * 19 /20
(res$variance[1]-res$variance[3]) * 19 /20
##############################
#### Example 14 - Biomass response
##############################
gdn <-
read.csv("http://www.math.montana.edu/courses/s217/documents/gundalebachnordin_2.csv")
gdn$Species <- factor(gdn$Species)
gdn$Treatment <- factor(gdn$Treatment)
qplot(
Species,
Massperha,
data = gdn,
facets = ~ Treatment,
main = "Influence of Nitrogen on growth ",
xlab = "Species",
ylab = "Mass per ha"
) +
geom_violin(aes(fill = Species)) +
geom_jitter(shape = 16, position = position_jitter(0.1)) +
theme(legend.position = "none")
### Look at classical linear regression with factors
fit <- aov(Massperha ~ Treatment + Species, data = gdn)
summary(fit)
fit$coefficients
fitlog <- aov(log(Massperha) ~ Treatment + Species, data = gdn)
summary(fitlog)
### Markov linear regression approach
number_col <- 2 # number of columns, her set to 4
number_lines <- numeric(number_col)
number_lines <-
c(2, 3) # first colum contains supp and second the factor dose
data <- data.frame(gdn[c(1, 2, 4)])
n <- nrow(data)
observation <- data
observation$Species <- as.numeric(observation$Species)
observation$Treatment <- as.numeric(observation$Treatment)
names(observation)[number_col + 1] <- "b"
T_U(observation, number_col, number_lines) -> TU
apply(TU, 2, function(x)
x[-1] - x[1])
fit$coefficients
# we can check if the experiment is balanced
Q_estimator(observation, number_col, number_lines)
T_QU(
observation,
number_col,
number_lines,
Q_estimator(observation, number_col, number_lines)
) -> TQU
TQU - TU
# We obtain the sames result; experiment is balanced and no bias.
######## Estimation of the variance
S_U(observation, number_col, number_lines) -> SU
SU
apply(SU, 2, function(x)
x[1] - x[-1])
S_QU(
observation,
number_col,
number_lines,
Q_estimator(observation, number_col, number_lines)
) -> SQU
SQU
apply(SU, 2, function(x)
x[1] - x[-1])
##############################
#### Example 15 - CASchools
##############################
library("AER")
library("MASS")
data(CASchools)
CASchools$STR <- CASchools$students/CASchools$teachers
CASchools$score <- (CASchools$read + CASchools$math)/2
cor(CASchools$STR, CASchools$score)
cor(CASchools$STR, CASchools$english)
### Plot the CASchool data
ggplot(data = CASchools, aes(x = STR, y = score)) +
geom_point(alpha = .8, aes(size = english)) +
labs(title = "The California test score data")
### Create categorical predictor variables
CASchools$STRCat <-
cut_number(CASchools$STR,
5,
labels = 1:5)
CASchools$englishCat <-
cut_number(
CASchools$english,
5,
labels = c("English 1", "English 2", "English 3", "English 4", "English 5")
)
ggplot(data = CASchools, aes(STRCat, score)) +
geom_violin(aes(fill = STRCat)) +
geom_jitter(shape = 16,
position = position_jitter(0.1),
size = 0.5) +
labs(title = "Influence on the score") +
facet_wrap(~ englishCat, ncol = 5)
#### multivariate linear regression
mult.modcat <- lm(score ~ STRCat + englishCat, data = CASchools)
summary(mult.modcat)
### Markov linear regression approach
CASchools$englishCat <-
cut_number(CASchools$english,
5,
labels = 1:5)
data <- (CASchools[c(18,17,16)])
number_col <- 2
number_lines <- c(5, 5)
data[1, ] <- as.numeric(data[1, ])
data[2, ] <- as.numeric(data[2, ])
names(data)[number_col + 1] <- "b"
summary(data)
T_U(data, number_col, number_lines) -> TU
apply(TU, 2, function(x)
x[-1] - x[1])
Q_estimator(data, number_col, number_lines) -> Q_est
Q_est
T_QU(data, number_col, number_lines, Q_est) -> TQU
apply(TQU, 2, function(x)
x[-1] - x[1])
######## Estimation of the variance
S_U(data, number_col, number_lines) -> SU
SU
apply(SU, 2, function(x)
x[1] - x[-1])
S_QU(data,
number_col,
number_lines,
Q_est) -> SQU
SQU
apply(SQU, 2, function(x)
x[1] - x[-1])
## Variances for each predictor
data %>%
group_by(englishCat) %>%
summarise(variance = sd(b)^2)
data %>%
group_by(STRCat) %>%
summarise(variance = sd(b)^2)