Skip to content

Commit c3da70d

Browse files
committed
update the inst/doc folder with 3.11.4 files
1 parent edf5390 commit c3da70d

19 files changed

+413
-433
lines changed

inst/doc/Chapter_AnimalTracking.R

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,7 @@ levels(loggerheadNoisy$turtle)
229229

230230

231231
###################################################
232-
### code chunk number 19: Cs17_kftrack (eval = FALSE)
233-
###################################################
234-
## library(kftrack) # must be installed from github
235-
## loggerhead <- loggerhead
236-
## turtlename <- "BigMama"
237-
## dat <- loggerhead[which(loggerhead$turtle == turtlename), 2:6]
238-
## model <- kftrack(dat,
239-
## fix.first = FALSE, fix.last = FALSE,
240-
## var.struct = "uniform"
241-
## )
242-
243-
244-
###################################################
245-
### code chunk number 21: Cs18_code
232+
### code chunk number 20: Cs18_code
246233
###################################################
247234
###############################################################
248235
# GCDF FUNCTION

inst/doc/Chapter_CombiningTrendData.R

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,83 +8,87 @@ library(MARSS)
88
### code chunk number 3: Cs001_readinredddata
99
###################################################
1010
head(okanaganRedds)
11-
logRedds = log(t(okanaganRedds)[2:3,])
11+
logRedds <- log(t(okanaganRedds)[c("aerial", "ground"), ])
1212

1313

1414
###################################################
1515
### code chunk number 4: Cs002_fig1
1616
###################################################
1717
# Code for plotting raw Okanagan redd counts
18-
plot(okanaganRedds[,1], okanaganRedds[,2],
19-
xlab = "", ylab="Redd counts",main="", col="red", pch=1)
20-
points(okanaganRedds[,1], okanaganRedds[,3], col="blue", pch=2)
21-
legend('topleft', inset=0.1, legend=c("Aerial survey","Ground survey"),
22-
col=c("red","blue"), pch=c(1,2))
18+
plot(okanaganRedds[, 1], okanaganRedds[, 2],
19+
xlab = "", ylab = "Redd counts", main = "", col = "red", pch = 1
20+
)
21+
points(okanaganRedds[, 1], okanaganRedds[, 3], col = "blue", pch = 2)
22+
legend("topleft",
23+
inset = 0.1, legend = c("Aerial survey", "Ground survey"),
24+
col = c("red", "blue"), pch = c(1, 2)
25+
)
2326

2427

2528
###################################################
2629
### code chunk number 5: Cs003_reddmodel1
2730
###################################################
28-
model1=list()
29-
model1$R="diagonal and equal"
30-
model1$Z=matrix(1,2,1)
31-
model1$A="scaling"
32-
kem1 = MARSS(logRedds, model=model1)
31+
model1 <- list()
32+
model1$R <- "diagonal and equal"
33+
model1$Z <- matrix(1, 2, 1)
34+
model1$A <- "scaling"
35+
kem1 <- MARSS(logRedds, model = model1)
3336

3437

3538
###################################################
3639
### code chunk number 6: Cs004_reddmodel2
3740
###################################################
38-
model2=model1 #model2 is based on model1
39-
model2$R="diagonal and unequal"
40-
kem2 = MARSS(logRedds, model=model2)
41+
model2 <- model1 # model2 is based on model1
42+
model2$R <- "diagonal and unequal"
43+
kem2 <- MARSS(logRedds, model = model2)
4144

4245

4346
###################################################
4447
### code chunk number 7: Cs005_reddmodel3
4548
###################################################
46-
model3=list()
47-
model3$Q="diagonal and equal"
48-
model3$R="diagonal and equal"
49-
model3$U="equal"
50-
model3$Z="identity"
51-
model3$A="zero"
52-
kem3 = MARSS(logRedds, model=model3)
49+
model3 <- list()
50+
model3$Q <- "diagonal and equal"
51+
model3$R <- "diagonal and equal"
52+
model3$U <- "equal"
53+
model3$Z <- "identity"
54+
model3$A <- "zero"
55+
kem3 <- MARSS(logRedds, model = model3)
5356

5457

5558
###################################################
5659
### code chunk number 8: Cs005b_aic
5760
###################################################
58-
c(mod1=kem1$AICc, mod2=kem2$AICc, mod3=kem3$AICc)
61+
c(mod1 = kem1$AICc, mod2 = kem2$AICc, mod3 = kem3$AICc)
5962

6063

6164
###################################################
6265
### code chunk number 9: Cs006_fig2
6366
###################################################
6467
# Code for plotting the fit from the best model
65-
plot(okanaganRedds[,1], logRedds[1,],
66-
xlab = "", ylab="Redd counts",main="", col="red", ylim=c(0,8))
67-
points(okanaganRedds[,1], logRedds[2,], col="blue", pch=2)
68-
lines(okanaganRedds[,1], c(kem1$states), lty=1, lwd=2)
69-
lines(okanaganRedds[,1], c(kem1$states + 2*kem1$states.se), lty=1, lwd=1, col="grey40")
70-
lines(okanaganRedds[,1], c(kem1$states - 2*kem1$states.se), lty=1, lwd=1, col="grey40")
68+
plot(okanaganRedds[, 1], logRedds[1, ], xlab = "", ylab = "Redd counts",
69+
main = "", col = "red", ylim = c(0, 8)
70+
)
71+
points(okanaganRedds[, 1], logRedds[2, ], col = "blue", pch = 2)
72+
lines(okanaganRedds[, 1], c(kem1$states), lty = 1, lwd = 2)
73+
lines(okanaganRedds[, 1], c(kem1$states + 2 * kem1$states.se), lty = 1, lwd = 1, col = "grey40")
74+
lines(okanaganRedds[, 1], c(kem1$states - 2 * kem1$states.se), lty = 1, lwd = 1, col = "grey40")
7175

7276

7377
###################################################
7478
### code chunk number 26: Cs007_birddata
7579
###################################################
76-
birddat = t(kestrel[,2:4])
80+
birddat <- t(kestrel[, c("British.Columbia", "Alberta", "Saskatchewan")])
7781
head(kestrel)
7882

7983

8084
###################################################
8185
### code chunk number 27: Cs008_plot-bird-data
8286
###################################################
8387
# Make a plot of the three time series
84-
plot(kestrel[,1], kestrel[,2], xlab = "", ylab="Index of kestrel abundance",main="", col="red",ylim=c(0,2), pch=21)
85-
points(kestrel[,1], kestrel[,3], col="blue", pch=22)
86-
points(kestrel[,1], kestrel[,4], col="purple", pch=25)
87-
legend('topright',inset=0.1, legend=c("British Columbia","Alberta","Saskatchewan"), col=c("red","blue","purple"), pch=c(21,22,25))
88+
plot(kestrel[, 1], kestrel[, 2], xlab = "", ylab = "Index of kestrel abundance", main = "", col = "red", ylim = c(0, 2), pch = 21)
89+
points(kestrel[, 1], kestrel[, 3], col = "blue", pch = 22)
90+
points(kestrel[, 1], kestrel[, 4], col = "purple", pch = 25)
91+
legend("topright", inset = 0.1, legend = c("British Columbia", "Alberta", "Saskatchewan"), col = c("red", "blue", "purple"), pch = c(21, 22, 25))
8892

8993

9094
###################################################
@@ -99,53 +103,56 @@ kem.b1 = MARSS(birddat, model=model.b1, control=list(minit=100) )
99103
###################################################
100104
### code chunk number 29: Cs011_fit-bird-model-2
101105
###################################################
102-
model.b2=list()
103-
model.b2$Q="diagonal and equal"
104-
model.b2$R="diagonal and equal"
105-
model.b2$Z="identity"
106-
model.b2$A="zero"
107-
model.b2$U="equal"
108-
kem.b2 = MARSS(birddat, model=model.b2)
106+
model.b2 <- list()
107+
model.b2$Q <- "diagonal and equal"
108+
model.b2$R <- "diagonal and equal"
109+
model.b2$Z <- "identity"
110+
model.b2$A <- "zero"
111+
model.b2$U <- "equal"
112+
kem.b2 <- MARSS(birddat, model = model.b2)
109113

110114

111115
###################################################
112116
### code chunk number 30: Cs013_fit-bird-model-3
113117
###################################################
114-
model.b3=model.b2 #is is based on model.b2
115-
#all we change is the structure of Q
116-
model.b3$Q="diagonal and unequal"
117-
model.b3$U="unequal"
118-
kem.b3 = MARSS(birddat, model=model.b3)
118+
model.b3 <- model.b2 # is is based on model.b2
119+
# all we change is the structure of Q
120+
model.b3$Q <- "diagonal and unequal"
121+
model.b3$U <- "unequal"
122+
kem.b3 <- MARSS(birddat, model = model.b3)
119123

120124

121125
###################################################
122126
### code chunk number 31: Cs015_fit-bird-model-4
123127
###################################################
124-
model.b4=list()
125-
model.b4$Q="diagonal and unequal"
126-
model.b4$R="diagonal and equal"
127-
model.b4$Z=factor(c("BC","AB-SK","AB-SK"))
128-
model.b4$A="scaling"
129-
model.b4$U="unequal"
130-
kem.b4 = MARSS(birddat, model=model.b4)
128+
model.b4 <- list()
129+
model.b4$Q <- "diagonal and unequal"
130+
model.b4$R <- "diagonal and equal"
131+
model.b4$Z <- factor(c("BC", "AB-SK", "AB-SK"))
132+
model.b4$A <- "scaling"
133+
model.b4$U <- "unequal"
134+
kem.b4 <- MARSS(birddat, model = model.b4)
131135

132136

133137
###################################################
134138
### code chunk number 32: Cs016_aics
135139
###################################################
136-
c(mod1=kem.b1$AICc, mod2=kem.b2$AICc, mod3=kem.b3$AICc, mod4=kem.b4$AICc)
140+
c(mod1 = kem.b1$AICc, mod2 = kem.b2$AICc, mod3 = kem.b3$AICc, mod4 = kem.b4$AICc)
137141

138142

139143
###################################################
140144
### code chunk number 33: Cs017_plot-bird-model-4-fits
141145
###################################################
142146
# Make a plot of the predicted trajectory, confidence intervals, and the raw data in log-space
143-
plot(kestrel[,1], kestrel[,2], xlab = "", ylab="Index of kestrel abundance",main="", col="red", ylim=c(0,2), pch=21)
144-
points(kestrel[,1], kestrel[,3], col="blue", pch=22)
145-
points(kestrel[,1], kestrel[,4], col="purple", pch=25)
146-
lines(kestrel[,1], c(kem.b4$states[1,]), lty=3, lwd=2, col="red")
147-
lines(kestrel[,1], c(kem.b4$states[2,]), lty=3, lwd=2, col="blue")
148-
lines(kestrel[,1], c(kem.b4$states[2,]+coef(kem.b4,type="matrix")$A[3,1]), lty=3, lwd=2, col="purple")
149-
legend('topright',inset=0.1, legend=c("British Columbia","Alberta","Saskatchewan"), col=c("red","blue","purple"), pch=c(21,22,25))
147+
plot(kestrel[, 1], kestrel[, 2], xlab = "", ylab = "Index of kestrel abundance",
148+
main = "", col = "red", ylim = c(0, 2), pch = 21)
149+
points(kestrel[, 1], kestrel[, 3], col = "blue", pch = 22)
150+
points(kestrel[, 1], kestrel[, 4], col = "purple", pch = 25)
151+
lines(kestrel[, 1], c(kem.b4$states[1, ]), lty = 3, lwd = 2, col = "red")
152+
lines(kestrel[, 1], c(kem.b4$states[2, ]), lty = 3, lwd = 2, col = "blue")
153+
lines(kestrel[, 1], c(kem.b4$states[2, ] + coef(kem.b4, type = "matrix")$A[3, 1]),
154+
lty = 3, lwd = 2, col = "purple")
155+
legend("topright", inset = 0.1, legend = c("British Columbia", "Alberta", "Saskatchewan"),
156+
col = c("red", "blue", "purple"), pch = c(21, 22, 25))
150157

151158

inst/doc/Chapter_Covariates.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ covariates <- rbind(
2222
Temp = fulldat[years, "Temp"],
2323
TP = fulldat[years, "TP"]
2424
)
25-
2625
# z.score the covariates
2726
covariates <- zscore(covariates)
2827

inst/doc/Chapter_KFAS.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ cbind(
174174
###################################################
175175
### code chunk number 19: Cs301_obs-filtering
176176
###################################################
177-
f_kfas <- KFS(fit_kfas$model,
177+
kf_kfas <- KFS(fit_kfas$model,
178178
filtering = "signal",
179179
smoothing = "signal", simplify = FALSE
180180
)
@@ -183,7 +183,7 @@ f_kfas <- KFS(fit_kfas$model,
183183
###################################################
184184
### code chunk number 20: Cs302_obs-filtering
185185
###################################################
186-
f_marss <- MARSSkf(fit_marss)
186+
kf_marss <- MARSSkf(fit_marss)
187187

188188

189189
###################################################
@@ -197,7 +197,7 @@ n <- 10
197197
###################################################
198198
ytt1_fit <- fitted(fit_marss, type = "ytt1")$.fitted
199199
ytt1_hatyt <- MARSShatyt(fit_marss, only.kem = FALSE)$ytt1
200-
cbind(m = f_kfas$m[1:n], fitted = ytt1_fit[1:n], MARSShatyt = ytt1_hatyt[1:n])
200+
cbind(m = kf_kfas$m[1:n], fitted = ytt1_fit[1:n], MARSShatyt = ytt1_hatyt[1:n])
201201

202202

203203
###################################################
@@ -208,7 +208,7 @@ var.Eytt1_fit <-
208208
var.Eytt1_hatyt <-
209209
MARSShatyt(fit_marss, only.kem = FALSE)$var.Eytt1
210210
cbind(
211-
P_mu = f_kfas$P_mu[1:n], fitted = var.Eytt1_fit[1:n],
211+
P_mu = kf_kfas$P_mu[1:n], fitted = var.Eytt1_fit[1:n],
212212
MARSShatyt = var.Eytt1_hatyt[1:n]
213213
)
214214

@@ -219,7 +219,7 @@ cbind(
219219
ytT_fit <- fitted(fit_marss, type = "ytT")$.fitted
220220
ytT_hatyt <- MARSShatyt(fit_marss)$ytT
221221
cbind(
222-
a = f_kfas$muhat[1:n], fitted = ytT_fit[1:n],
222+
a = kf_kfas$muhat[1:n], fitted = ytT_fit[1:n],
223223
MARSShatyt = ytT_hatyt[1:n], Nile = Nile[1:n]
224224
)
225225

@@ -232,7 +232,7 @@ var.EytT_fit <-
232232
var.EytT_hatyt <-
233233
MARSShatyt(fit_marss, only.kem = FALSE)$var.EytT
234234
cbind(
235-
V_mu = f_kfas$V_mu[1:n], fitted = var.EytT_fit[1:n],
235+
V_mu = kf_kfas$V_mu[1:n], fitted = var.EytT_fit[1:n],
236236
MARSShatyt = var.EytT_hatyt[1:n]
237237
)
238238

@@ -371,7 +371,8 @@ head(df)
371371
###################################################
372372
### code chunk number 50: Cs502_plotting
373373
###################################################
374-
plot(fit_marss, plot.type = "fitted.ytT", pi.int = TRUE)
374+
plot.type <- ifelse(packageVersion("MARSS") < '3.11.4', "model.ytT", "fitted.ytT")
375+
plot(fit_marss, plot.type = plot.type, pi.int = TRUE)
375376

376377

377378
###################################################

inst/doc/Chapter_MLR.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,8 @@ xyplot(Reaction ~ Days | Subject, sleepstudy,
244244
# number of subjects
245245
nsub <- length(unique(sleepstudy$Subject))
246246
ndays <- length(sleepstudy$Days) / nsub
247-
# each subject is a row with day across the columns
248247
dat <- matrix(sleepstudy$Reaction, nsub, ndays, byrow = TRUE)
249248
rownames(dat) <- paste("sub", unique(sleepstudy$Subject), sep = ".")
250-
# the day number 0 to 9 is the explanatory variable
251249
exp.var <- matrix(sleepstudy$Days, 1, ndays, byrow = TRUE)
252250

253251

inst/doc/Chapter_PVA.R

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,59 +93,50 @@ params[nsim + 2, ] <- c(sim.u, sim.u, sim.Q, sim.R, sim.Q)
9393
###################################################
9494
# Needs Example 2 to be run first
9595
par(mfrow = c(3, 3))
96-
pd <- 0.1
97-
xd <- -log(pd) # decline threshold
98-
te <- 100
99-
tyrs <- 1:te # extinction time horizon
96+
pd <- 0.1; xd <- -log(pd) # decline threshold
97+
te <- 100; tyrs <- 1:te # extinction time horizon
10098
for (j in c(10, 1:8)) {
10199
real.ex <- denn.ex <- kal.ex <- matrix(nrow = te)
102100

103101
# MARSS parameter estimates
104-
u <- params[j, 1]
105-
Q <- params[j, 3]
102+
u <- params[j, 1]; Q <- params[j, 3]
106103
if (Q == 0) Q <- 1e-4 # just so the extinction calc doesn't choke
107104
p.ever <- ifelse(u <= 0, 1, exp(-2 * u * xd / Q))
108105
for (i in 1:100) {
109106
if (is.finite(exp(2 * xd * abs(u) / Q))) {
110-
sec.part <- exp(2 * xd * abs(u) / Q) * pnorm((-xd - abs(u) * tyrs[i]) / sqrt(Q * tyrs[i]))
111-
} else {
112-
sec.part <- 0
113-
}
107+
sec.part <- exp(2 * xd * abs(u) / Q) *
108+
pnorm((-xd - abs(u) * tyrs[i]) / sqrt(Q * tyrs[i]))
109+
} else { sec.part <- 0 }
114110
kal.ex[i] <- p.ever * pnorm((-xd + abs(u) * tyrs[i]) / sqrt(Q * tyrs[i])) + sec.part
115111
} # end i loop
116112

117113
# Dennis et al 1991 parameter estimates
118-
u <- params[j, 2]
119-
Q <- params[j, 5]
114+
u <- params[j, 2]; Q <- params[j, 5]
120115
p.ever <- ifelse(u <= 0, 1, exp(-2 * u * xd / Q))
121116
for (i in 1:100) {
122117
denn.ex[i] <- p.ever * pnorm((-xd + abs(u) * tyrs[i]) / sqrt(Q * tyrs[i])) +
123-
exp(2 * xd * abs(u) / Q) * pnorm((-xd - abs(u) * tyrs[i]) / sqrt(Q * tyrs[i]))
118+
exp(2 * xd * abs(u) / Q) *
119+
pnorm((-xd - abs(u) * tyrs[i]) / sqrt(Q * tyrs[i]))
124120
} # end i loop
125121

126122
# True parameter values
127-
u <- sim.u
128-
Q <- sim.Q
123+
u <- sim.u; Q <- sim.Q
129124
p.ever <- ifelse(u <= 0, 1, exp(-2 * u * xd / Q))
130125
for (i in 1:100) {
131126
real.ex[i] <- p.ever * pnorm((-xd + abs(u) * tyrs[i]) / sqrt(Q * tyrs[i])) +
132-
exp(2 * xd * abs(u) / Q) * pnorm((-xd - abs(u) * tyrs[i]) / sqrt(Q * tyrs[i]))
127+
exp(2 * xd * abs(u) / Q) *
128+
pnorm((-xd - abs(u) * tyrs[i]) / sqrt(Q * tyrs[i]))
133129
} # end i loop
134130

135-
# plot it
136-
plot(tyrs, real.ex,
137-
xlab = "Time steps into future",
138-
ylab = "Probability of extinction", ylim = c(0, 1), bty = "l"
139-
)
131+
plot(tyrs, real.ex, xlab = "Time steps into future",
132+
ylab = "Probability of extinction", ylim = c(0, 1), bty = "l")
140133
if (j <= 8) title(paste("simulation ", j))
141134
if (j == 10) title("average over sims")
142135
lines(tyrs, denn.ex, type = "l", col = "red", lwd = 2, lty = 1)
143136
lines(tyrs, kal.ex, type = "l", col = "green", lwd = 2, lty = 2)
144137
}
145-
legend("bottomright", c("True", "Dennis", "KalmanEM"),
146-
pch = c(1, -1, -1),
147-
col = c(1, 2, 3), lty = c(-1, 1, 2), lwd = c(-1, 2, 2), bty = "n"
148-
)
138+
legend("bottomright", c("True", "Dennis", "KalmanEM"), pch = c(1, -1, -1),
139+
col = c(1, 2, 3), lty = c(-1, 1, 2), lwd = c(-1, 2, 2), bty = "n")
149140

150141

151142
###################################################

0 commit comments

Comments
 (0)