library(Bolstad)
par(mar = c(2, 2, 2, 2))

Studi Kasus-1

## Pembangkitan data x dan y

set.seed(1312)
x = runif(100, min=1, max=5)
b0 = 4.5
b1 = 2.5
y = b0 + b1*x + rnorm(100)

data.frame(x,y)
plot(x,y,xlab ="Nilai X",ylab="Nilai Y")

## Menggunakan prior flat untuk slope.prior (beta1)
## Menggunakan prior flat untuk intcpt.prior (beta0)
bayes.lin.reg(y,x, slope.prior = "flat", intcpt.prior = "flat", alpha = 0.05, plot.data = TRUE)
## Standard deviation of residuals:  0.99
##             Posterior Mean Posterior Std. Deviation
##             -------------- ------------------------
## Intercept:  11.94          0.099036                
## Slope:      2.457          0.084019

## Memprediksi y berdasarkan data x yang baru: x = 4, 1, 2
bayes.lin.reg(y,x, slope.prior = "flat", intcpt.prior = "flat", pred.x=c(4, 1, 2))
## Standard deviation of residuals:  0.99
##             Posterior Mean Posterior Std. Deviation
##             -------------- ------------------------
## Intercept:  11.94          0.099036                
## Slope:      2.457          0.084019

## x       Predicted y  SE         
## ------  -----------  -----------
## 4         14.41        0.99887    
## 1         7.04         1.0093     
## 2         9.497        0.9988

Studi Kasus-2

## Pembangkitan data x dan y
set.seed(1312)
x = rnorm(100, mean = 3, sd = 4)
y = 4.5 + 2.5*x + rnorm(100)

data.frame(x,y)
plot(x,y,xlab ="Nilai X",ylab="Nilai Y")

## Menggunakan prior untuk slope.prior Normal(mean=0, sd=2)
## Menggunakan prior untuk intcpt.prior Normal(mean=1,sd=3)
bayes.lin.reg(y,x, slope.prior = "normal", intcpt.prior = "normal", 0, 2, 1, 3, alpha = 0.05, plot.data = TRUE)
## Standard deviation of residuals:  1.07
##             Posterior Mean Posterior Std. Deviation
##             -------------- ------------------------
## Intercept:  12.45          0.1069                  
## Slope:      2.461          0.029955

## Memprediksi y berdasarkan data x yang baru: x = 3, -1, 6
bayes.lin.reg(y,x, slope.prior = "normal", intcpt.prior = "normal", 0, 2, 1, 3, pred.x=c(3, -1, 6))
## Standard deviation of residuals:  1.07
##             Posterior Mean Posterior Std. Deviation
##             -------------- ------------------------
## Intercept:  12.45          0.1069                  
## Slope:      2.461          0.029955

## x       Predicted y  SE         
## ------  -----------  -----------
## 3         11.81        1.075      
## -1        1.966        1.0825     
## 6         19.2         1.0781

Studi Kasus-3

## The heart rate vs. oxygen uptake: Bolstad dan Curran (2017), hlm. 303
OU = c(0.47,0.75,0.83,0.98,1.18,1.29,1.40,1.60,1.75,1.90,2.23)
HR = c(94,96,94,95,104,106,108,113,115,121,131)

plot(HR,OU,xlab="Heart Rate",ylab="Oxygen uptake (Percent)")

bayes.lin.reg(OU,HR,slope.prior = "normal", intcpt.prior = "flat",0,1,sigma=0.13)
## Known standard deviation:  0.13
##             Posterior Mean Posterior Std. Deviation
##             -------------- ------------------------
## Intercept:  1.307          0.039196                
## Slope:      0.04265        0.0033723

.