diff --git a/TMB examples/REML/REML.R b/TMB examples/REML/REML.R new file mode 100644 index 0000000..8cc3136 --- /dev/null +++ b/TMB examples/REML/REML.R @@ -0,0 +1,62 @@ + +setwd( "C:/Users/James.Thorson/Desktop/Project_git/2018_FSH556/TMB examples/REML" ) +#devtools::install_github("kaskr/TMB_contrib_R/TMBhelper") + +library(lme4) +library(TMB) + +# Compile model +Version = "REML" +compile( paste0(Version,".cpp") ) +dyn.load( dynlib("REML") ) + +###################### +# Simulate data +###################### + +# Simulate predictors +group_i = rep( 1:10, each=10) +z_g = rnorm( length(unique(group_i)), mean=0, sd=1) +beta0 = 0 + +# Simulate response +y_i = z_g[group_i] + beta0 + rnorm( length(group_i), mean=0, sd=1) + +###################### +# Run WITHOUT REML in R and TMB +###################### + +# In R +Lme_noREML = lmer( y_i ~ 1|factor(group_i), REML=FALSE) + +# In TMB +Data = list( "n_groups"=length(unique(group_i)), "g_i"=group_i-1, "y_i"=y_i) +Parameters = list( "beta0"=-10, "log_SD0"=2, "log_SDZ"=2, "z_g"=rep(0,Data$n_groups) ) +Random = c("z_g") +Obj = MakeADFun(data=Data, parameters=Parameters, random=Random) # +Opt_noREML = TMBhelper::Optimize( obj=Obj, newtonsteps=1 ) + +# TMB +exp(Opt_noREML$par['log_SDZ']) +# R +summary(Lme_noREML) + +###################### +# Run WITH REML in R and TMB +###################### + +# In R +Lme_REML = lmer( y_i ~ 1|factor(group_i), REML=TRUE) + +# In TMB +Data = list( "n_groups"=length(unique(group_i)), "g_i"=group_i-1, "y_i"=y_i) +Parameters = list( "beta0"=-10, "log_SD0"=2, "log_SDZ"=2, "z_g"=rep(0,Data$n_groups) ) +Random = c("z_g","beta0") +Obj = MakeADFun(data=Data, parameters=Parameters, random=Random) # +Opt_REML = TMBhelper::Optimize( obj=Obj, newtonsteps=1 ) + +# TMB +exp(Opt_REML$par['log_SDZ']) +# R +summary(Lme_REML) + diff --git a/TMB examples/REML/REML.cpp b/TMB examples/REML/REML.cpp new file mode 100644 index 0000000..83489dd --- /dev/null +++ b/TMB examples/REML/REML.cpp @@ -0,0 +1,40 @@ +#include +template +Type objective_function::operator() () +{ + // Data + DATA_INTEGER( n_groups ); + DATA_IVECTOR( g_i ); + DATA_VECTOR( y_i ); + + // Parameters + PARAMETER( beta0 ); + PARAMETER( log_SD0 ); + PARAMETER( log_SDZ ); + PARAMETER_VECTOR( z_g ); + + // Objective funcction + Type jnll = 0; + int n_i = y_i.size(); + + // Probability of data conditional on fixed and random effect values + for( int i=0; i