patperry / r-mbest

Moment-Based Estimation for Hierarchical Models (R Package)
Apache License 2.0
24 stars 6 forks source link

Allowing for formula variables, fixing summary call from models with do.call #5

Closed kschmaus closed 7 years ago

kschmaus commented 7 years ago

Currently mbest doesn't seem to be able to handle variables used for formulas (see the attempt to fit the model2 object below). If the formula isn't known until call time, one can use the do.call function as a work around. This presents another issue when one calls the summary method on the model object. In addition to the usual output, source code as well as a printout of any data.frame data argument are also printed out. For models fit on large datasets, this output can become overwhelming.

Either allowing for model formula variables, or resolving the summary call from models fit with do.call would be beneficial extensions.

Example

library(mbest)
print(version)
# platform       x86_64-apple-darwin13.4.0   
# arch           x86_64                      
# os             darwin13.4.0                
# system         x86_64, darwin13.4.0        
# status                                     
# major          3                           
# minor          3.2                         
# year           2016                        
# month          10                          
# day            31                          
# svn rev        71607                       
# language       R                           
# version.string R version 3.3.2 (2016-10-31)
# nickname       Sincere Pumpkin Patch   
packageVersion("mbest")
# [1] ‘0.5’

sleepstudy <- lme4::sleepstudy

# fit model
model1 <- mhglm(Reaction ~ Days + (Days | Subject), gaussian, sleepstudy)

# fit model with formula variable
model_formula <- Reaction ~ Days + (Days | Subject)
model2 <- mhglm(model_formula, gaussian, sleepstudy)
# Error in mhglm(model_formula, gaussian, sleepstudy) : 
#   Invalid grouping factor specification, Subject
# In addition: Warning message:
# In Ops.factor(Days, Subject) : ‘|’ not meaningful for factors

# fit model with do.call 
model_args <- list(
  formula = Reaction ~ Days + (Days | Subject),
  family = gaussian,
  data = sleepstudy
)
model3 <- do.call(mhglm, model_args)

summary(model1)
summary(model3) # will be very long