jmcurran / Bolstad

0 stars 0 forks source link

bayes.lin.reg error #1

Open jmcurran opened 6 years ago

jmcurran commented 6 years ago

I'm obviously doing something wrong in using the R function bayes.lin.reg, using question 2 from the assignment.

My data frame (q2) is simple: 

x y
0 78
0 82
0 75
0 58
2 75
2 42
2 50
2 55
4 27
4 48
4 49
4 39

When I do an ordinary regression, I get a reasonable result:

reslm <- lm(y ~ x, q2) reslmCall: lm(formula = y ~ x, data = q2)

     Coefficients:
     (Intercept)       72.750       
              x           -8.125  

But I'm getting a strange intercept when I use bayes.lin.reg, no matter what I try. Here's a very simple call, assuming flat priors:

 > results2 <- bayes.lin.reg(y=q2$y,x=q2$x)
 Standard deviation of residuals:  11.2             
                   Posterior Mean Posterior Std. Deviation            
                   --------------   ------------------------
 Intercept:  4.962                0.95508                 
    Slope:      -1.66                0.89203     

Actually, that slope isn't right, either. I get similar results for the intercept if I use the parameters specified in question 2, but I wanted to show here the simplest call I could (which, or course, lead to poor predictions for x=3). Here, with more complicated priors, I get a good slope but still a poor intercept and predictions:

results2 <- bayes.lin.reg(y=q2$y,x=q2$x,slope.prior="normal", mb0=0,sb0=10,sigma=12,pred.x=c(0,1,2,3,4,5,6,7,8))Known standard deviation:  12             Posterior Mean Posterior Std. Deviation            -------------- ------------------------Intercept:  4.962          0.95508                 Slope:      -7.82          1.9363                  x       Predicted y  SE         ------  -----------  -----------0         20.6         12.646     1         12.78        12.193     2         4.962        12.038     3         -2.859       12.193     4         -10.68       12.646     5         -18.5        13.366     6         -26.32       14.314     7         -34.14       15.448     8         -41.96       16.73      > Obviously, I'm messing up the call in some way, but after a couple of hours I haven't figured out what.

jmcurran commented 6 years ago

Okay - I know why this happens now. bayes.lin.reg calls bayes.lm after trying to work out what the values for the prior mean and prior variance/covariance matrix should be. It should, at the very least, turn off the centering of the explanatory variables (and will do this shortly). It should also just set prior = NULL when both the slope and the intercept have a flat prior. The issue how should we sensibly deal with mixed priors, e.g. flat for intercept, normal for slope, or vice versa?