提问人:Josh 提问时间:11/12/2023 更新时间:11/13/2023 访问量:15
具有多个物种和多个地点的MARSS模型
MARSS model with multiple species and multiple sites
问:
我一直在试图弄清楚如何正确格式化 MARSS 模型(使用 R 中的 MARSS 包),以便如果可能的话,我可以同时在多个地点对多个物种进行建模。在我的完整研究设计中,我每年都会从 9 个不同地点采集 30 个物种的样本。我已经能够让模型运行在一个具有多个物种的站点以及多个站点和只有一个物种的站点(请参阅下面的 reprex,尽管我不完全确定这些编码是否正确),但无法弄清楚如何将它们放在一起,或者这是否是可以做到的。如果这是一种合适的方法,我也可以为每个站点运行单独的模型。理想情况下,我希望为我的每个站点获取单独的 B 矩阵,以便我可以比较站点之间的社区稳定性指标。如果有人有任何想法(或者如果我做错了),我将不胜感激!
library(MARSS)
dat <- data.frame(site = rep(1:2,each = 20), # two independent sites
year = rep(c(1:20),2), # sample years (1-20)
spp1 = sample(50,40, replace = T), # simulated count data species 1
spp2 = sample(20,40, replace = T), # simulated count data species 2
spp3 = sample(30,40, replace = T)) # simulated count data species 3
# Put count data into wide format (rows = species, columns = years)
site1 <- t(dat[1:20,3:5])
site2 <- t(dat[21:40,3:5])
rownames(site1) <- paste0("site1_", rownames(site1))
rownames(site2) <- paste0("site2_", rownames(site2))
y <- rbind(site1,site2)
# RUN THE MODEL FOR ONE SPECIES AND BOTH SITES
y1 <- rbind(y[1,], y[4,])
# Z-matrix formatting
z.model1 <- matrix(c(1,0,0,1),ncol = 2)
# A-matrix formatting
a.model1 <- matrix(list(0),2,1)
a.model1[2,1] <- c("b")
# U-matrix formatting
u.model1 <- matrix(list(0),2,1)
u.model1[1:2,1] <- c("a","b")
# Put together all parts of model
model.1 <- list(Z = z.model1,
A = a.model1,
Q = "diagonal and unequal",
R = "diagonal and equal",
U = u.model1,
tinitx = 1)
# Run the model
m1.out <- MARSS(y1, model = model.1, method = "BFGS")
# NOW RUN THE MODEL WITH ALL THREE SPECIES BUT ONLY ONE SITE
y2 <- y[1:3,]
# Z-matrix formatting
z.model2 <- matrix(0,3,3)
diag(z.model2) <- 1
# U-matrix formatting
u.model2 <- matrix(list(0),3,1)
u.model2[1:3,1] <- c("a","b","c")
# B-matrix formatting
b.model2 <- matrix(c("b11","b12","b13",
"b21","b22","b23",
"b31","b32","b33"),
ncol = 3)
# Put together all parts of model
model.2 <- list(Z = z.model2,
B = b.model2,
Q = "diagonal and unequal",
R = "diagonal and equal",
U = u.model2,
tinitx = 1)
# Run the model
m2.out <- MARSS(y2, model = model.2, method = "BFGS")
答:
0赞
Josh
11/13/2023
#1
我可能刚刚弄清楚了我的问题(见下面的代码)。我会把这个问题留出来,以防有人知道我是否做错了。
# Using the same data as in the original question
y <- rbind(site1,site2)
y
# Create needed matrices (doing this manually for clarity)
# B-matrix formatting
b.model <- matrix(list(0),6,6)
b.model[1:3,1:3] <- c("A.b11","A.b12","A.b13", # Each site has a 3x3 "matrix" embedded within
"A.b21","A.b22","A.b23", # the overall 6x6 matrix. Not sure this is an
"A.b31","A.b32","A.b33") # appropriate way of doing this, the plan would
# be to isolate out the individual B-matrices
b.model[4:6,4:6] <- c("B.b11","B.b12","B.b13", # for each site from the "overall B-matrix"
"B.b21","B.b22","B.b23",
"B.b31","B.b32","B.b33")
# Z-matrix formatting
z.model <- matrix(0,6,6)
diag(z.model) <- 1 # same as "identity"
z.model
# Q-matrix formatting
q.model <- matrix(list(0),6,6)
diag(q.model) <- c("Qsp1A","Qsp2A","Qsp3A",
"Qsp1B","Qsp2B","Qsp3B") # same as "diagonal & unequal"
q.model
# R-matrix formatting
r.model <- matrix(0,6,6) # same as "zero" (for now considering detection as being perfect)
# U-matrix formatting
u.model <- matrix(c("Usp1A","Usp2A","Usp3A", # same as "unconstrained"
"Usp1B","Usp2B","Usp3B"),
6,1)
# Put together all parts of model
model.0 <- list(Z = z.model,
B = b.model,
Q = q.model,
R = r.model,
U = u.model,
tinitx = 0)
# Run the model
m0.out <- MARSS(y, model = model.0, method = "BFGS")
m0.B <- coef(m0.out, type = "matrix")$B[1:6, 1:6]
m0.B
# Separating out individual site's B-matrices
# Again, not sure this is appropriate/ok to do...
m0.B.siteA <- m0.B[1:3,1:3]
m0.B.siteB <- m0.B[4:6,4:6]
max(abs(eigen(m0.B.siteA)$values))
max(abs(eigen(m0.B.siteB)$values))
上一个:将矩阵的数字快速提高到序列的幂?
下一个:按行从矩阵中抽样
评论