This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
forked from James-Thorson/2018_FSH556
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added REML example to "TMB_examples"
- Loading branch information
1 parent
90335d1
commit 947e11b
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <TMB.hpp> | ||
template<class Type> | ||
Type objective_function<Type>::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<n_i; i++){ | ||
jnll -= dnorm( y_i(i), beta0 + z_g(g_i(i)), exp(log_SD0), true ); | ||
} | ||
|
||
// Probability of random coefficients | ||
for( int g=0; g<n_groups; g++){ | ||
jnll -= dnorm( z_g(g), Type(0.0), exp(log_SDZ), true ); | ||
} | ||
|
||
// Reporting | ||
Type SDZ = exp(log_SDZ); | ||
Type SD0 = exp(log_SD0); | ||
|
||
REPORT( SDZ ); | ||
REPORT( SD0 ); | ||
ADREPORT( SDZ ); | ||
ADREPORT( SD0 ); | ||
|
||
return jnll; | ||
} |