Repository navigation
Using the tmb_map argument #101
Unanswered
PeriGerson
asked this question in
Q&A
Replies: 2 comments 4 replies
|
Try this instead: library(tinyVAST)
library(fmesher)
set.seed(101)
options("tinyVAST.verbose" = FALSE)
# Simulate settings
theta_xy = 0.4
n_x = n_y = 10
n_c = 5
rho = 0.8
resid_sd = 0.5
# Simulate GMRFs
R_s = exp(-theta_xy * abs(outer(1:n_x, 1:n_y, FUN="-")) )
R_ss = kronecker(X=R_s, Y=R_s)
delta_fs = mvtnorm::rmvnorm(n_c, sigma=R_ss )
#
L_cf = matrix( rnorm(n_c^2), nrow=n_c )
L_cf[,3:5] = 0
L_cf = L_cf + resid_sd * diag(n_c)
#
d_cs = L_cf %*% delta_fs
# Shape into longform data-frame and add error
Data = data.frame( expand.grid(species=1:n_c, x=1:n_x, y=1:n_y),
"var"="logn", "z"=exp(as.vector(d_cs)) )
Data$n = tweedie::rtweedie( n=nrow(Data), mu=Data$z, phi=0.5, power=1.5 )
mean(Data$n==0)
# make mesh
mesh = fm_mesh_2d( Data[,c('x','y')] )
#
sem = "
f1 -> 1, l1
f1 -> 2, l2
f1 -> 3, l3
f1 -> 4, l4
f1 -> 5, l5
f2 -> 2, l6
f2 -> 3, l7
f2 -> 4, l8
f2 -> 5, l9
f1 <-> f1, NA, 1
f2 <-> f2, NA, 1
1 <-> 1, NA, 0
2 <-> 2, NA, 0
3 <-> 3, NA, 0
4 <-> 4, NA, 0
5 <-> 5, NA, 0
"
# fit model
out = tinyVAST( space_term = sem,
data = Data,
formula = n ~ 0 + factor(species),
spatial_domain = mesh,
family = tweedie(),
variables = c( "f1", "f2", 1:n_c ),
space_columns = c("x","y"),
variable_column = "species",
time_column = "time",
distribution_column = "dist",
control = tinyVASTcontrol(gmrf="proj") )
out
# copy MLE
parhat <- out$internal$parlist
# create mapping list with NA in the factors for fixed parameters
tmb_map <- out$tmb_inputs$tmb_map
#
fixed_effects = unique(names(out$opt$par))
# Turn off all fixed effects
for( name in fixed_effects ){
tmp = list(factor(rep(NA,length(parhat[[name]]))))
names(tmp) = name
tmb_map = c(tmb_map, tmp)
}
# Build model with all fixed effects mapped off
refit_model = tinyVAST( space_term = sem,
data = Data,
formula = n ~ 0 + factor(species),
spatial_domain = mesh,
family = tweedie(),
variables = c( "f1", "f2", 1:n_c ),
space_columns = c("x","y"),
variable_column = "species",
time_column = "time",
distribution_column = "dist",
control = tinyVASTcontrol(gmrf="proj",
tmb_par = parhat,
tmb_map = tmb_map,
nlminb_loops = 0,
getsd = FALSE) )
# Optimize random effects via call to fn(.)
refit_model$obj$fn()
# Should match original fit if using all data
# Important as sanity check
out$opt$obj
##########
# NOW REDO WITH WEIGHTS
##########
# randomly assign data for cross validation
Data$fold <- sample(1:5, nrow(Data), replace = TRUE)
# assign weight based on fold (UPDATED ... was previously using weight = 1 for all samples)
Data$weight = ifelse(Data$fold == 1, 0, 1)
#Data$weight = rep( 1, nrow(Data) )
# fit model using weights and fixed fixed effects
refit_model = tinyVAST( space_term = sem,
data = Data,
formula = n ~ 0 + factor(species),
spatial_domain = mesh,
family = tweedie(),
variables = c( "f1", "f2", 1:n_c ),
space_columns = c("x","y"),
variable_column = "species",
time_column = "time",
distribution_column = "dist",
weights = Data$weight,
control = tinyVASTcontrol(gmrf="proj",
tmb_par = parhat,
tmb_map = tmb_map,
nlminb_loops = 0,
getsd = FALSE) )
# Should NOT match original fit
out$opt$obj
refit_model$obj$fn() |
2 replies
|
Please try my code snippet again ... I had commented out the weight assignment for each fold while exploring |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi Jim,
I am working on comparing several models in tinyVAST using cross validation. We discussed fitting a base model to the complete dataset and then using those parameter estimates for the fixed effects in the cross validation to help expedite and stabilize the fitting process. I was trying this out with one of your vignettes and was hoping you could confirm that I have it right. Please see example code below. Thanks so much!
All reactions