/
predict.R
161 lines (128 loc) · 5.54 KB
/
predict.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
# === Predictions ---------------------------------------------------------
# Predict: means ----------------------------------------------------------
predict_means <- function(DT, model) {
# Take DT, return means for all variables
means <- DT[, .(
sl_ = mean(sl_, na.rm = TRUE),
temp = mean(temp, na.rm = TRUE),
brickdist_end = 0,
edgedist_end = 0,
indiv_treat_step_id = NA
), by = .(id_treat, ghostbricks, stage)]
# Predict hab for each ghostbricks*stage combination
# Uses cbind with .BY[[N]] to retain ghostbricks and stage in the model
# Be careful with .BY[[N]], depends on the order specified in by =
means[, hab := predict(model,
newdata = cbind(.SD,
id_treat = .BY[[1]],
ghostbricks = .BY[[2]],
stage = .BY[[3]]),
type = "link",
re.form = NULL),
by = .(id_treat, ghostbricks, stage)]
return(means)
}
# Predict: brick dist ------------------------------------------------------
predict_brickdist <- function(DT, model) {
# Take DT, return means for all variables except for seq 0:max brickdist
bdist <- DT[, .(
sl_ = mean(sl_, na.rm = TRUE),
temp = mean(temp, na.rm = TRUE),
brickdist_end = seq(0, max(brickdist_end), length.out = 100),
edgedist_end = mean(edgedist_end, na.rm = TRUE),
indiv_treat_step_id = NA
), by = .(ghostbricks, stage, id_treat)]
# Predict hab for each ghostbricks*stage combination
# Uses cbind with .BY[[N]] to retain ghostbricks and stage in the model
# Be careful with .BY[[N]], depends on the order specified in by =
bdist[, hab := predict(model,
newdata = cbind(.SD,
id_treat = .BY[[1]],
ghostbricks = .BY[[2]],
stage = .BY[[3]]),
type = "link",
re.form = NULL),
by = .(id_treat, ghostbricks, stage)]
return(bdist)
}
# Predict: edge dist ------------------------------------------------------
predict_edgedist <- function(DT, model) {
# Take DT, return means for all variables except for seq 0:max edgedist
edist <- DT[, .(
sl_ = mean(sl_, na.rm = TRUE),
temp = mean(temp, na.rm = TRUE),
brickdist_end = mean(brickdist_end, na.rm = TRUE),
edgedist_end = seq(0, max(edgedist_end), length.out = 100),
indiv_treat_step_id = NA
), by = .(id_treat, ghostbricks, stage)]
# Predict hab for each ghostbricks*stage combination
# Uses cbind with .BY[[N]] to retain ghostbricks and stage in the model
# Be careful with .BY[[N]], depends on the order specified in by =
edist[, hab := predict(model,
newdata = cbind(.SD,
id_treat = .BY[[1]],
ghostbricks = .BY[[2]],
stage = .BY[[3]]),
type = "link",
re.form = NULL),
by = .(id_treat, ghostbricks, stage)]
return(edist)
}
# Predict: speed ----------------------------------------------------------
predict_speed <- function(coefs, seqs) {
if (is.null(coefs)) return(NULL)
bdist <- seqs$bdist
edist <- seqs$edist
logsltemp <- seqs$logsltemp
logsltod <- seqs$logsltod
meanedge <- seqs$meanedge
maxedge <- seqs$maxedge
meanbrick <- seqs$meanbrick
maxbrick <- seqs$maxbrick
meantemp <- seqs$meantemp
pred_length <- 100
repcoef <- coefs[rep(.N, pred_length)]
repcoef[, bd.spd.before :=
(1 + logsl_ + logsl_before + (logsltemp * meantemp) + logsltod +
(brickdist_logsl_before * bdist) +
(edgedist_logsl_before * meanedge)) * (1 / rate),
by = .(id, brick)]
repcoef[, bd.spd.after :=
(1 + logsl_ + logsl_after + (logsltemp * meantemp) + logsltod +
(brickdist_logsl_after * bdist) +
(edgedist_logsl_after * meanedge)) * (1 / rate),
by = .(id, brick)]
repcoef[, bdist := seq(0, maxbrick, length.out = 100),
by = .(id, brick)]
repcoef[, ed.spd.before :=
(1 + logsl_ + logsl_before + (logsltemp * meantemp) + logsltod +
(edgedist_logsl_before * edist) +
(brickdist_logsl_before * meanbrick)) * (1 / rate),
by = .(id, brick)]
repcoef[, ed.spd.after :=
(1 + logsl_ + logsl_after + (logsltemp * meantemp) + logsltod +
(edgedist_logsl_after * edist) +
(brickdist_logsl_after * meanbrick)) * (1 / rate),
by = .(id, brick)]
repcoef[, edist := seq(0, maxedge, length.out = 100),
by = .(id, brick)]
repcoef[, disturbance := ifelse(brick %like% 'g', 'undisturbed', 'disturbed'),
by = .(id, brick)]
return(repcoef)
}
# Make prediction sequence ------------------------------------------------
make_predict_seq <- function(combtreats, model) {
maxedge <- max(combtreats$edgedist_end, na.rm = TRUE)
maxbrick <- 65
list(
bdist = seq(0, maxbrick, length.out = 100),
edist = seq(0, maxedge, length.out = 100),
logsltemp = model$estimate[[2]],
logsltod = model$estimate[[3]],
meanedge = mean(combtreats$edgedist_end, na.rm = TRUE),
maxedge = maxedge,
meanbrick = mean(combtreats$brickdist_end, na.rm = TRUE),
maxbrick = maxbrick,
meantemp = mean(combtreats$temp, na.rm = TRUE)
)
}