Error using stan_lmer to estimate betas varying by group without intercepts (0 + beta | group)

47 views
Skip to first unread message

Steven McAlpine

unread,
Apr 5, 2017, 12:49:13 PM4/5/17
to Stan users mailing list
When I use lme4 syntax in stan_lmer to specify a model that estimates varying betas by group without varying intercepts by group:

try(students_regression_stan_lmer <- stan_lmer(post_test_with_treatment_effect ~ pre_test + treated + (0 + treated| class), 
                                  data = students_observed_data, cores = 4, chains = 4, iter = 1000))

I receive this error:

## failed to create the sampler; sampling not done
"Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  Exception thrown at line -1: []: accessing element out of range. index 251 out of range; expecting index to be between 1 and 250; index position = 1v
failed to create the sampler; sampling not done
Error in check_stanfit(stanfit) : 
  Invalid stanfit object produced please report bug

However the same syntax works with lmer:

students_regression3_lmer <- lmer(post_test_with_treatment_effect ~ pre_test + treated + (0 + treated| class), 
                                  data = students_observed_data)

And I can get estimated group level treatment effects:

ranef(students_regression3_lmer)

## $class
##       treated
## 1   0.0000000
## 2   0.0000000
## 3   0.0000000
## 4   0.0000000
## 5   0.0000000
## 6   0.0000000
## 7   0.0000000
## 8   0.0000000
## 9   0.0000000
## 10  0.0000000
## 11 -0.7559308
## 12 -1.2623717
## 13 -9.5061368
## 14  1.9232203
## 15  3.3363638
## 16  3.7266542
## 17  4.3228975
## 18 -1.4900363
## 19  1.8620994
## 20 -2.1567597 


If I include the intercept I can estimate both the varying intercepts and coefficients with stan_lmer:

try(students_regression_stan_lmer_ints <- stan_lmer(post_test_with_treatment_effect ~ pre_test + treated + (1 + treated| class), 
                                               data = students_observed_data, cores = 4, chains = 4, iter = 1000))

ranef(students_regression_stan_lmer_ints)

$class
   (Intercept)      treated
1   0.63580565  0.049151831
2   0.71091557  0.156797701
3   0.57515724  0.079141737
4  -0.39207768 -0.061048210
5  -0.26561929 -0.008566484
6  -0.21244440 -0.047024221
7  -0.02676781  0.069140913
8  -1.55592209 -0.253629955
9   0.46025720  0.039753356
10  0.10868246  0.031543260
11 -0.24217318 -0.485766410
12  0.40094964  0.867404777
13 -1.47314199 -3.158838203
14  0.05938561  0.242515555
15  0.34507101  0.781582205
16 -0.16358781 -0.369586202
17  0.41345505  0.997931713
18  1.01236945  2.311212882
19  0.27806014  0.685134304
20 -0.93531046 -1.911475304

However I was wondering if there is a way for me to specify the common intercept and but varying beta model in stan_lmer.

Thanks for the help,
Steven

Code block to create example:

require(rstanarm)
require(lme4)

students <- data.frame(id = 1:(25*20), class = rep(1:20, 25)) #assigning students to class
class_pre_effects <- rnorm(20, 50, 10) #estimating class pre test intercepts
students <- merge(students, data.frame(class = 1:20, alpha_j = class_pre_effects), "class") #assigning intercepts to students
students$error_pre <- rnorm( 25*20, 0, 15) #adding student errors
students$pre_test <- students$alpha_j + students$error_pre #calculating student pre-test scores
students$post_test_pre_treatment <- 10 + 0.7 * students$pre_test + rnorm( 25*20, 0, 10) ##simulating post test scores before adding treatment effects
class_treatment_effects <- rnorm(20, 5, 5) ##calculating treatment effects
class_treatment_effects[1:10] <- 0  ##removing effects for control classes (1 to 10)
students <- merge(students, data.frame(class = 1:20, class_treatment_effects), "class") ##combining data and flagging treated classes
students$treated <- ifelse(students$class > 10, 1, 0)
students$post_test_with_treatment_effect <- students$post_test_pre_treatment + students$class_treatment_effects ##calculating post test scores with the treatment effect
students_observed_data <- students[ ,c("id", "class", "pre_test", "post_test_with_treatment_effect", "treated")] ##building a dataset with only 'observed' data

try(students_regression_stan_lmer <- stan_lmer(post_test_with_treatment_effect ~ pre_test + treated + (0 + treated| class), 
                                  data = students_observed_data, cores = 4, chains = 4, iter = 1000))
try(print(students_regression_stan_lmer))
try(fixef(students_regression_stan_lmer))
try(ranef(students_regression_stan_lmer))

students_regression3_lmer <- lmer(post_test_with_treatment_effect ~ pre_test + treated + (0 + treated| class), 
                                  data = students_observed_data)

print(students_regression3_lmer)
fixef(students_regression3_lmer)
ranef(students_regression3_lmer)


try(students_regression_stan_lmer_ints <- stan_lmer(post_test_with_treatment_effect ~ pre_test + treated + (1 + treated| class), 
                                               data = students_observed_data, cores = 4, chains = 4, iter = 1000))

print(students_regression_stan_lmer_ints)
fixef(students_regression_stan_lmer_ints)
ranef(students_regression_stan_lmer_ints)

Ben Goodrich

unread,
Apr 5, 2017, 12:55:38 PM4/5/17
to Stan users mailing list
On Wednesday, April 5, 2017 at 12:49:13 PM UTC-4, Steven McAlpine wrote:
When I use lme4 syntax in stan_lmer to specify a model that estimates varying betas by group without varying intercepts by group:

try(students_regression_stan_lmer <- stan_lmer(post_test_with_treatment_effect ~ pre_test + treated + (0 + treated| class), 
                                  data = students_observed_data, cores = 4, chains = 4, iter = 1000))

I receive this error:

Someone mentioned a similar error, but I can't remember if it was fixed. Can you follow the instructions at

https://github.com/stan-dev/rstanarm/

to see if the development version of rstanarm works with your data.

Thanks,
Ben

Steven McAlpine

unread,
Apr 5, 2017, 1:48:23 PM4/5/17
to Stan users mailing list
Worked with the dev version, thanks Ben.

> try(ranef(students_regression_stan_lmer))
$class
        treated
1  -0.013134458
2  -0.033457459
3  -0.059072184
4   0.061699865
5   0.042507496
6  -0.004144876
7  -0.006677640
8  -0.001202708
9   0.124420175
10 -0.008293905
11  2.584949514
12 -6.804695059
13 -4.630114704
14  0.660090230
15 -4.798647205
16  6.988000708
17 -6.463076759
18  3.180010161
19  3.508330587
20  5.566320395

-Steven
Reply all
Reply to author
Forward
0 new messages