DHGLM with two (nested) random intercept

Hi!

I have repeated behavioural data on multiple indivdiuals which are nested in different groups (colonies). My goal is to test among-individual and within-individual (residual) variation in behaviour and also control for colony effect.

I’m trying to build a double hierarchical mixed model, with the following dataset:

data.list =
list(y = (xdata$totdist-mean(xdata$totdist)) / sd(xdata$totdist),
ind = as.integer(xdata$ind),
colony = as.integer(xdata$colid),
headsize	= (xdata$head-mean(xdata$head)) / sd(xdata$head),
colsize = (xdata$size-mean(xdata$size)) / sd(xdata$size),
myrnest = (xdata$myrnest-mean(xdata$myrnest)) / sd(xdata$myrnest),
totnest = (xdata$totnest-mean(xdata$totnest)) / sd(xdata$totnest),
habitat = as.numeric(xdata$habitat),
N = length(xdata$totdist),	
N_ind = length(unique(xdata$ind)),
N_col = length(unique(xdata$colid)))

with the following stan code:

data{
	int<lower=1> N;
	int<lower=1> N_ind;
	int<lower=1> N_col;
	real y[N];
	real headsize[N];
	real colsize[N];
	real myrnest[N];
	real totnest[N];
	int habitat[N];
	int colony[N];
	int ind[N];
}
parameters{
	vector[2] re_id[N_ind];
	vector[N_col] re_col;
	vector[6] b;
	vector[6] g;
	vector<lower=0,upper=10>[2] sd_id;
	real<lower=0,upper=10> sd_col;
	corr_matrix[2] id_cor;
}
model{
	vector[N] sdy;
	vector[N] muy;

	id_cor ~ lkj_corr(2);
	
// Priors for random effect variances commented out as set in parameters.
	//sd_id ~ uniform(0, 10); 	//prior implied in ‘parameters’ block
	//sd_col ~ uniform(0, 10);

	b ~ normal(0,100);		// prior for mean fixed effects
	g ~ normal(0,100);		// prior for residual fixed effects

//setup random effect term
	re_id ~ multi_normal( rep_vector(0,2), quad_form_diag(id_cor,sd_id));
	re_col ~ normal(0, sd_col);

///////////// Setup of the likelihood model ///////////////////

for ( i in 1:N ) {

// Mean level model

muy[i] = (b[1] + re_id[ind[i],1] +  re_col[colony[i]]) + b[2]*headsize[i] + 
	     b[3]*colsize[i]+ b[4]*myrnest[i] + b[5]*totnest[i] + 
	     b[6]*habitat[i];

// Residual level model
// Log-link moved to right-hand side of equation as the exponential

sdy[i] = exp(g[1] + g[2]*headsize[i] + g[3]*colsize[i] + g[4]*myrnest[i] +
		  b[5]*totnest[i] + b[6]*habitat[i] + re_id[ind[i],2]);
}
y ~ normal( muy , sdy );
}

Although after running the model, I get the following Error message

here are whatever error messages were returned
[[1]]
Stan model ‘activity’ does not contain samples.

[[2]]
Stan model ‘activity’ does not contain samples.

[[3]]
Stan model ‘activity’ does not contain samples.

Warning message:
In .local(object, …) :
some chains had errors; consider specifying chains = 1 to debug

Could you help me to figure out what the problem could be? I’m a newby with Stan and at this point I have no idea what’s wrong.

Thanks in advance.

Hi and welcome. Can you post the code you ran your model with?

Hi and thanks for the quick response

Sorry for being a bit sloppy with posting my data structure before. I have my own data file, here are the first 21 lines to get the basic impression (note: I don’t use all of the variables)

xdata<-read.table("Myrmica data_NEW.txt", header=T,sep="\t",fill=T, dec = ".")      # Import .txt

xdata$habitat<-as.factor(xdata$treat)
xdata$ind<-as.factor(xdata$ind)
xdata$colid<-as.factor(xdata$colid)
xdata$head<-as.numeric(xdata$head)

   treat colid col size totnest myrnest totprod work gyne male    ind rep repi    head  sdhead totdist   mspeed  mmeand totsq newsq agr timeerg habitat
1     1k   K11   1 1636       2       2     994  860  131    3  K11.B   1   12 1068.55 1068.55 239.468 0.804124 568.307    31    18   1       5      1k
2     1k   K11   1 1636       2       2     994  860  131    3  K11.B   2   12 1068.55 1068.55 260.528 0.868427 116.928    18     8   3       8      1k
3     1k   K11   1 1636       2       2     994  860  131    3  K11.B   3   12 1068.55 1068.55 199.567 0.665225 139.460    41    22   0       5      1k
4     1k   K11   1 1636       2       2     994  860  131    3  K11.G   1   10 1045.89 1045.89 272.998 0.913047 280.213    30    18   3      16      1k
5     1k   K11   1 1636       2       2     994  860  131    3  K11.G   2   10 1045.89 1045.89 304.315 1.013720 438.077    27    17   3       9      1k
6     1k   K11   1 1636       2       2     994  860  131    3  K11.G   3   10 1045.89 1045.89 300.938 1.002470 164.041    50    18   3       0      1k
7     1k   K11   1 1636       2       2     994  860  131    3  K11.P   1    8 1012.20 1012.20 199.415 0.664281 499.351    24    15   3      25      1k
8     1k   K11   1 1636       2       2     994  860  131    3  K11.P   2    8 1012.20 1012.20 285.880 0.952934 136.795    15    10   2       0      1k
9     1k   K11   1 1636       2       2     994  860  131    3  K11.P   3    8 1012.20 1012.20 276.260 0.920867 164.162    49    22   3       2      1k
10    1k   K11   1 1636       2       2     994  860  131    3 K11.PB   1    7 1054.04 1054.04 272.208 0.926508 652.080     0     0   2     180      1k
11    1k   K11   1 1636       2       2     994  860  131    3 K11.PB   2    7 1054.04 1054.04 130.572 0.435240 165.579    34    20   3       7      1k
12    1k   K11   1 1636       2       2     994  860  131    3 K11.PB   3    7 1054.04 1054.04 133.913 0.446377 706.576    28    16   3      11      1k
13    1k   K11   1 1636       2       2     994  860  131    3 K11.PG   1    6 1007.69 1007.69 254.531 0.847880 204.768    14    12   3      10      1k
14    1k   K11   1 1636       2       2     994  860  131    3 K11.PG   2    6 1007.69 1007.69 190.918 0.635974 306.400    33    14   2       2      1k
15    1k   K11   1 1636       2       2     994  860  131    3 K11.PG   3    6 1007.69 1007.69 192.442 0.641053 357.066    35    17   2       4      1k
16    1k   K11   1 1636       2       2     994  860  131    3 K11.PP   1    4  987.14  987.14 374.167 1.246400 125.099     0     0   1     180      1k
17    1k   K11   1 1636       2       2     994  860  131    3 K11.PP   2    4  987.14  987.14 319.991 1.066640 101.105    23    18   3      11      1k
18    1k   K11   1 1636       2       2     994  860  131    3 K11.PP   3    4  987.14  987.14 332.335 1.107060 236.396    58    22   3       0      1k
19    1k   K11   1 1636       2       2     994  860  131    3 K11.PY   1    5 1094.75 1094.75 334.490 1.117950 231.831    10     6   1      44      1k
20    1k   K11   1 1636       2       2     994  860  131    3 K11.PY   2    5 1094.75 1094.75 259.420 0.864166 426.920    21    16   2      13      1k
21    1k   K11   1 1636       2       2     994  860  131    3 K11.PY   3    5 1094.75 1094.75 248.018 0.826726 642.477    34    21   2       2      1k

this is how I create the a data list I work with

 standardise the variables

meanuse = apply(cbind(xdata$totdist,xdata$head,xdata$size,xdata$myrnest,xdata$totnest),2,mean) 
# finds the mean values for total distance moved, head size, colony size, nr. of myrmica nests and nr. of all nests

sduse = apply(cbind(xdata$totdist,xdata$head,xdata$size,xdata$myrnest,xdata$totnest),2,sd)
# finds the SD for total distance moved, head size, colony size, nr. of myrmica nests and nr. of all nests

## define variables

data.list =
list(y 		= (xdata$totdist-meanuse[1])/sduse[1], # standardized total distance
	colony 	= as.integer(xdata$colid),	 	
	ind		= as.integer(xdata$ind),
	headsize 	= (xdata$head-meanuse[2])/sduse[2], # standardized head size
	colsize 	= (xdata$size-meanuse[3])/sduse[3], # standardized colony size
	myrnest 	= (xdata$myrnest-meanuse[4])/sduse[4], # standardized nr. of myrmica nests
	totnest 	= (xdata$totnest-meanuse[5])/sduse[5], # standardized nr. of all nests 
	habitat	= as.numeric(xdata$habitat),
	N		= length(xdata$totdist), # total sample size
	Nind		= length(unique(xdata$ind)),
	Ncol		= length(unique(xdata$colid)))


after creating the stan code you can see above (activity.stan), I proceed with the following script in rstan

model <- stan(file="activity.stan", data = data.list,
             iter=60000, warmup=10000, chains=3, 
             pars=c("log_lik"), thin=5,          
             control=list(max_treedepth = 12)) 

Hope that helps. Thak you very much!

I’ll dive into the no data thing in a second. A few things stand out on your model call
iter should be something like 2000 and reduce the warmup and thin to it’s default setting. If you can you set the number of cores to what ever the max value is. I have 8 on my laptop and typically run with cores = 6.