Search code examples
rmcmc

How do i sample the posterior distribution for the regression coefficients?


I am trying to figure out where I am making a mistake in this question?

library(MCMCpack)
library(mcmcplots)
X=c(66, 51, 60, 48, 48, 34, 48, 46, 51, 58, 51, 62, 43, 42, 44, 50, 57, 52, 40, 42)
Y=c(75.8, 56.8, 68.0, 55.4, 56.4, 44.2, 57.4, 53.8, 63.8, 67.4, 58.8, 66.6, 55.4, 49.6, 59.2, 58.0, 64.6, 58.6, 54.0, 57.6)
data<-list(X,Y)
post= MCMCregress(dist~speed, data=data)
summary(post)
plot(post, col=c("red"))
denplot(post)

I keep getting the error message-

Error in eval(predvars, data, env) : object 'speed' not found

Thank you in advance


Solution

  • I get the error you posted when I call the following part of your code:

    post= MCMCregress(dist~speed, data=data)
    

    This is because in data you have given the names X and Y. I can successfully run that line when I change it to the following:

    post= MCMCregress(Y~X, data=data)
    

    I then have to change the next line, as I get an error when using concatenate. I change it to this and it works:

    plot(post, col="red")
    

    The last line works for me without error.