lm in RStanARM

147 views
Skip to first unread message

Peter Li

unread,
Jul 25, 2013, 3:35:48 PM7/25/13
to stan...@googlegroups.com, Li.Pe...@gmail.com
I'm trying to run a simple model using RStanARM,
> stanlm (kid_score ~ mom_hs, data=kidiq)
but it's giving me this error
Error in model.frame.default(formula = formula, data = data, subset = subset,  : 
  invalid type (closure) for variable '(weights)'
Is this the proper way to call stanlm? [I used the same format as I would for lm since the readme file says you can call stanlm in the same way]


Peter

Ben Goodrich

unread,
Jul 25, 2013, 3:53:04 PM7/25/13
to stan...@googlegroups.com, Li.Pe...@gmail.com
That is a bug. For the time being, you can just call stanglm with the (default) Gaussian likelihood

stanglm(kid_score ~ mom_hs, data=kidiq)

which is all stanlm() is supposed to be doing anyway.

Ben

Peter Li

unread,
Jul 25, 2013, 3:56:33 PM7/25/13
to Ben Goodrich, stan...@googlegroups.com, Li.Pe...@gmail.com
Calling
> stanglm (kid_score ~ mom_hs, data=kidiq)
gives
Error in apply(x[, -1], 2, FUN = function(x) { : 
  dim(X) must have a positive length

Ben Goodrich

unread,
Jul 25, 2013, 4:02:41 PM7/25/13
to stan...@googlegroups.com, Li.Pe...@gmail.com
Another bug. I will push fixes for both in a minute. -- Ben

Ben Goodrich

unread,
Jul 25, 2013, 4:07:13 PM7/25/13
to stan...@googlegroups.com, Li.Pe...@gmail.com
On Thursday, July 25, 2013 4:02:41 PM UTC-4, Ben Goodrich wrote:
Another bug. I will push fixes for both in a minute. -- Ben

Try now, which requires a reinstall.

Ben
 

Peter Li

unread,
Jul 25, 2013, 4:20:00 PM7/25/13
to Ben Goodrich, stan...@googlegroups.com, Li.Pe...@gmail.com
Now,
> stanlm(kid_score ~ mom_pred, data=dataList.1)
gives
Error in eval(expr, envir, enclos) : 
  mismatch in number dimensions declared and found in context; processing stage=data initialization; dims declared=(1); dims found=()
failed to create the sampler; sampling not done
Error in dimnames(stanfit)$parameters : 
  $ operator is invalid for atomic vectors
I checked the dimensions of mom_hs and kid_score, and they're both 434.


Peter

Ben Goodrich

unread,
Jul 25, 2013, 4:45:33 PM7/25/13
to stan...@googlegroups.com, Li.Pe...@gmail.com
That is some other bug with models that only have 1 predictor that I'll try to track down later tonight. -- Ben

Peter Li

unread,
Jul 25, 2013, 5:09:14 PM7/25/13
to Ben Goodrich, stan...@googlegroups.com, Li.Pe...@gmail.com
Alright. The same error exists for stanglm. Everything else seems to be working so far. I'll let you know if anything else comes up.


Peter

Ben Goodrich

unread,
Jul 26, 2013, 7:13:57 PM7/26/13
to stan...@googlegroups.com, Li.Pe...@gmail.com
Mav, do you have an idea as to what would cause this error. When you do something with 1 predictor and a constant, like

fit <- stanglm(RTEN ~ INTG, data = USJudgeRatings)

the standata list looks like this before calling stan()

debug: stanfit <- rstan:::stan(fit = stanfit, data = standata, init = start,
   
...)
Browse[2]> str(standata)
List of 17
 $ N                        
: int 43
 $ K                        
: int 2
 $ X                        
: num [1:43, 1:2] 1 1 1 1 1 1 1 1 1 1 ...
 
..- attr(*, "dimnames")=List of 2
 
.. ..$ : chr [1:43] "AARONSON,L.H." "ALEXANDER,J.M." "ARMENTANO,A.J." "BERDON,R.I." ...
 
.. ..$ : chr [1:2] "(Intercept)" "INTG"
 
..- attr(*, "assign")= int [1:2] 0 1
 $ y                        
: Named num [1:43] 7.8 8.7 7.8 8.7 4.8 8.6 9 5 8.8 7.9 ...
 
..- attr(*, "names")= chr [1:43] "AARONSON,L.H." "ALEXANDER,J.M." "ARMENTANO,A.J." "BERDON,R.I." ...
 $ family                    
: int 2
 $ link                      
: int 1
 $ has_weights              
: int 0
 $ weights                  
: num [1:43] 1 1 1 1 1 1 1 1 1 1 ...
 $ has_offset                
: int 0
 $ offset                    
: num [1:43] 0 0 0 0 0 0 0 0 0 0 ...
 $ prior_scale              
: num 3.57
 $ prior_scale_for_intercept
: num 22
 $ prior_mean                
: num 0
 $ prior_mean_for_intercept  
: num 0
 $ prior_df                  
: num 1
 $ prior_df_for_intercept    
: num 1
 $ prior_scale_for_dispersion
: num 5

which yields a mismatch

Error : mismatch in number dimensions declared and found in context; processing stage=data initialization; dims declared=(1); dims found=()

failed to create the sampler
; sampling not done

but when there is more than one predictor, then it is fine

fit <- stanglm(RTEN ~ INTG + ORAL, data = USJudgeRatings)

despite the standata list being essentially the same

Browse[2]> str(standata)
List of 17
 $ N                        
: int 43
 $ K                        
: int 3
 $ X                        
: num [1:43, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
 
..- attr(*, "dimnames")=List of 2
 
.. ..$ : chr [1:43] "AARONSON,L.H." "ALEXANDER,J.M." "ARMENTANO,A.J." "BERDON,R.I." ...
 
.. ..$ : chr [1:3] "(Intercept)" "INTG" "ORAL"
 
..- attr(*, "assign")= int [1:3] 0 1 2
 $ y                        
: Named num [1:43] 7.8 8.7 7.8 8.7 4.8 8.6 9 5 8.8 7.9 ...
 
..- attr(*, "names")= chr [1:43] "AARONSON,L.H." "ALEXANDER,J.M." "ARMENTANO,A.J." "BERDON,R.I." ...
 $ family                    
: int 2
 $ link                      
: int 1
 $ has_weights              
: int 0
 $ weights                  
: num [1:43] 1 1 1 1 1 1 1 1 1 1 ...
 $ has_offset                
: int 0
 $ offset                    
: num [1:43] 0 0 0 0 0 0 0 0 0 0 ...
 $ prior_scale              
: num [1:2] 3.57 2.73
 $ prior_scale_for_intercept
: num 22
 $ prior_mean                
: num [1:2] 0 0
 $ prior_mean_for_intercept  
: num 0
 $ prior_df                  
: num [1:2] 1 1
 $ prior_df_for_intercept    
: num 1
 $ prior_scale_for_dispersion
: num 5

Only X is different and it just has one more column, so I don't know why that would make a difference in this data block

# GLM for a Gaussian outcome via Stan
data
{
 
int<lower=1> N; # number of observations
 
int<lower=1> K; # number of predictors
  matrix
[N,K]  X; # predictor matrix
  vector
[N]    y; # continuous outcome

 
# link function from location to linear predictor
 
int<lower=1,upper=3> link; # 1 = identity, 2 = log, 3 = inverse

 
int<lower=0,upper=1> has_weights; # 0 = No (weights is a ones vector), 1 = Yes
  vector
[N] weights;

 
int<lower=0,upper=1> has_offset;  # 0 = No (offset is a zero vector), 1 = Yes
  vector
[N] offset;

 
# values for hyperparameters
  vector
<lower=0>[K-1] prior_scale;
  real
<lower=0> prior_scale_for_intercept;
  vector
[K-1] prior_mean;
  real prior_mean_for_intercept
;
  vector
<lower=0>[K-1] prior_df;
  real
<lower=0> prior_df_for_intercept;
  real
<lower=0> prior_scale_for_dispersion;
}

Ben

Jiqiang Guo

unread,
Jul 26, 2013, 8:18:11 PM7/26/13
to stan...@googlegroups.com
I think when K = 2, there are elements below are vector of length 1.  By default, a vector of length 1 in R corresponds to a scalar in Stan.  To fix this, you need to make those data explicitly arrays in R such as 

df <- 2
prior_df <- array(df, dim = c(length(df))

--
Jiqiang

Ben Goodrich

unread,
Jul 26, 2013, 9:00:48 PM7/26/13
to stan...@googlegroups.com
Thanks, fix pushed now.

Ben

Ben Goodrich

unread,
Jul 29, 2013, 12:45:55 PM7/29/13
to stan...@googlegroups.com
Hi Mav,

For glm(), this works

glm(RTEN > 8 ~ INTG + ORAL + WRIT, data = USJudgeRatings, family = binomial())

but not for stanglm()

> stanglm(RTEN > 8 ~ INTG + ORAL + WRIT, data = USJudgeRatings, family = binomial())
Error : variable does not exist; processing stage=data initialization; variable name=y; base type=int
In addition: Warning message:
In FUN(c("N", "K", "X", "y", "family", "link", "has_weights", "weights",  :
  data
with name y is not numeric and not used

failed to create the sampler
; sampling not done

although this works

stanglm(as.integer(RTEN > 8) ~ INTG + ORAL + WRIT, data = USJudgeRatings, family = binomial())

I could fix this in stanglm(), but is there a reason why stan() doesn't convert logical vectors to integer vectors?

Ben

Jiqiang Guo

unread,
Jul 29, 2013, 12:58:25 PM7/29/13
to stan...@googlegroups.com
My original thought is make the data preprocessing do least implicit transformation.  But later we added some, some of which are suggestion from you.  I guess converting from logical (TRUE/FALSE) to 1/0 is reasonable.  I will add them and doc them. 

--
Jiqiang 
 



Ben

--
You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages