# Run single model with grouping variable for habitat
# to show how to obtain predictions for different values
# of a continuous covariate (PpnGrass) by group (habitat)
# Make design data
mallard.ddl <- make.design.data(mallard.pr)
# run 1 model with proportion grass and habitat
ppn.gr.hab = mark(mallard.pr,
mallard.ddl,
nocc=90,
model="Nest",
model.parameters=list(S=list(formula = ~PpnGrass + habitat)))
# generate range of values of PpnGrass to use for predictions
min.PpnGrass = min(mallard$PpnGrass)
max.PpnGrass = max(mallard$PpnGrass)
PpnGrass.values = seq(from = min.PpnGrass, to = max.PpnGrass, length = 100)
# Find 1st index value for each group
first.row.for.group = c(
min(which(mallard.ddl$S$habitat == "Native")),
min(which(mallard.ddl$S$habitat == "Planted")),
min(which(mallard.ddl$S$habitat == "Roadside")),
min(which(mallard.ddl$S$habitat == "Wetland")))
# make covariate predictions for various values of PpnGrass
# in each habitat type
pred.PpnGrass.hab <- covariate.predictions(ppn.gr.hab,
data=data.frame(PpnGrass=PpnGrass.values),
indices = first.row.for.group)
head(pred.PpnGrass.hab$estimates)
# Note 'covdata' represents "PpnGrass", and par.index relates to 'habitat',
# but it's helpful to do more work to label columns
library(dplyr)
# rename 'covdata' as 'PpnGrass'
preds <- pred.PpnGrass.hab$estimates %>%
rename(PpnGrass = covdata)
head(preds)
# merge 'habitat' values in from mallard.ddl based on 'par.index'
preds2 <- left_join(preds,
mallard.ddl$S %>%
dplyr::select(par.index, habitat))
head(preds2)
Users browsing this forum: No registered users and 7 guests