Bayesian Analysis Module 2
Bayesian Analysis Module 2
[1]
Descriptions
Hypothesis testing vs. parameter estimation [2,3]
Debate on the process [4]
Bayes Factor (K) [3]
• Analogous to p-value or full vs reduced
model
• Way to formally compare two competing
models [5]
• Nesting not necessary
• Ratio of posterior odds for M1 to prior
odds for M1 -> so factor >> 1 supports
M1 over M2
Highest density interval analogous to
MLE with 95% CI
[6]
Descriptions 2
Items needed for analysis [7]
• Priors
• Model
• Data
Steps for analysis
• Covered in Module III
Posterior simulation details
• Covered in Module III
Specific tests
T-test
[8]
Specific tests
[9]
ANOVA
Specific tests
Regression
[10]
Examples setup
#Standard
t.test(diffScores)
Bayes factor analysis Bayes factor analysis
-------------- --------------
#Bayes [1] Alt., r=0.707 : 17.25888 ±0% [1] Null, mu=0 : 0.05794119 ±0%
bf_t <- ttestBF(x = diffScores)
bf_t #alternative model Against denominator: Against denominator:
1/bf_t #null model Null, mu = 0 Alternative, r = 0.707106781186548, mu =/= 0
--- ---
Bayes factor type: BFoneSample, JZS Bayes factor type: BFoneSample, JZS
Step-by-step Example 1.1
T-test cont.
#Chains #Multiple
chains <-posterior(bf_t, iterations=1000) bfInterval <- ttestBF(x = diffScores, nullInterval=c(-Inf,0))
summary(chains) allbf <- c(bf_t, bfInterval)
chains2 <- recompute(chains, iterations = 10000) allbf
plot(chains2[,1:2]) plot(allbf)
Against denominator:
Null, mu = 0
---
Bayes factor type: BFoneSample, JZS
Step-by-step Example 1.2
ANOVA
#ANOVA
#Data formatting
plot(chickwts$weight~chickwts$feed)
#Standard
summary(aov(weight~feed, data=chickwts))
#Bayes
bf_a <-anovaBF(weight~feed, data=chickwts)
bf_a Bayes factor analysis
--------------
[1] feed : 14067867 ±0%
Df Sum Sq Mean Sq F value Pr(>F)
feed 5 231129 46226 15.37 5.94e-10 *** Against denominator:
Residuals 65 195556 3009 Intercept only
--- ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Bayes factor type: BFlinearModel, JZS
Step-by-step Example 1.2
ANOVA cont.
#Chains
chains <-posterior(bf_a, iterations=1000)
summary(chains)
plot(chains[,2:7])
1. Empirical mean and standard deviation for each variable, 2. Quantiles for each variable:
Iterations = 1:1000
plus standard error of the mean:
Thinning interval = 1
Mean SD Naive SE Time-series SE 2.5% 25% 50% 75% 97.5%
Number of chains = 1
mu 259.35 6.588 0.20832 0.21197 mu 246.9212 254.834 259.295 263.856 272.199
Sample size per chain = 1000
feed-casein 59.04 14.627 0.46255 0.46255 feed-casein 29.0577 50.076 59.399 68.161 87.889
feed-horsebean -90.99 15.253 0.48233 0.31331 feed-horsebean -120.4751 -101.600 -90.733 -81.078 -60.935
feed-linseed -37.51 14.129 0.44680 0.47422 feed-linseed -65.4949 -47.219 -37.210 -27.900 -10.219
feed-meatmeal 17.22 15.118 0.47808 0.47808 feed-meatmeal -12.8971 7.509 17.343 27.791 45.609
feed-soybean -12.92 13.261 0.41937 0.42755 feed-soybean -39.2842 -21.363 -13.029 -4.321 13.042
feed-sunflower 65.15 14.635 0.46281 0.48797 feed-sunflower 35.7072 55.543 65.632 75.919 92.326
sig2 3138.97 571.281 18.06548 19.90381 sig2 2231.9416 2727.824 3084.627 3440.836 4512.308
g_feed 1.60 1.446 0.04572 0.04572 g_feed 0.3711 0.800 1.219 1.876 5.006
Step-by-step Example 1.3
Simple Linear Regression
#Simple Linear Regression Call:
#Data formatting lm(formula = rating ~ complaints, data = attitude)
data(attitude)
Residuals:
par(mfrow=c(1,1))
Min 1Q Median 3Q Max
plot(x=attitude$complaints, y=attitude$rating) -12.8799 -5.9905 0.1783 6.2978 9.6294
#Standard Coefficients:
lm1 = lm(rating ~ complaints, data = attitude) Estimate Std. Error t value Pr(>|t|)
summary(lm1) (Intercept) 14.37632 6.61999 2.172 0.0385 *
complaints 0.75461 0.09753 7.737 1.99e-08 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
https://und.qualtrics.com/jfe/form/SV_eW1CNrs7cPhjqFE
Step-by-step Example 2.1
Standard versus Bayesian Analysis in SAS
T-test
DATA Iris; set sashelp.Iris;
PROC FREQ data=Iris;
tables Species;
PROC MEANS data=Iris;
where Species in ("Setosa" "Versicolor");
var SepalLength;
by Species;
histogram SepalLength /group=Species transparency=0.30; Analysis Variable : SepalLength Sepal Length (mm)
N Mean Std Dev Minimum Maximum
Iris Species
50 50.0600000 3.5248969 43.0000000 58.0000000
Cumulative Cumulative
Species Frequency Percent Frequency Percent Iris Species=Versicolor
Setosa 50 33.33 50 33.33 Analysis Variable : SepalLength Sepal Length (mm)
Versicolor 50 33.33 100 66.67 N Mean Std Dev Minimum Maximum
Virginica 50 33.33 150 100.00 50 59.3600000 5.1617115 49.0000000 70.0000000
Step-by-step Example 2.1
Analysis Of Maximum Likelihood Parameter Estimates
T-test cont. Standard Wald 95% Wald Chi- Pr > ChiS
PROC GENMOD data=Iris; Parameter DF Estimate Error Confidence Limits Square q
Intercept 1 59.3600 0.6188 58.1472 60.5728 9203.20 <.0001
where Species in ("Setosa" "Versicolor");
Species Setosa 1 -9.3000 0.8751 -11.0151 -7.5849 112.95 <.0001
class Species;
Species Versicolor 0 0.0000 0.0000 0.0000 0.0000 . .
model SepalLength=Species;
Scale 1 4.3753 0.3094 3.8091 5.0257
Posterior Intervals
Parameter Alpha Equal-Tail Interval HPD Interval
Intercept 0.050 58.1180 60.6000 58.1440 60.6221
SpeciesSetosa 0.050 -11.0525 -7.5646 -11.1072 -7.6300
Dispersion 0.050 15.0091 26.5130 14.8495 26.0907
Step-by-step Example 2.1
T-test cont. 2
PROC GENMOD data=Iris;
where Species in ("Setosa" "Versicolor");
class Species;
model SepalLength=Species;
bayes seed=1 coeffprior=normal;
Step-by-step Example 2.2
ANOVA Analysis Of Maximum Likelihood Parameter Estimates
PROC SGPLOT data=Iris; Standard Wald 95% Wald Chi- Pr > ChiS
Parameter DF Estimate Error Confidence Limits Square q
vbox SepalLength /group=Species;
Intercept 1 65.8800 0.7207 64.4674 67.2926 8355.87 <.0001
Species Setosa 1 -15.8200 1.0192 -17.8177 -13.8223 240.92 <.0001
PROC GENMOD data=Iris;
Species Versicolor 1 -6.5200 1.0192 -8.5177 -4.5223 40.92 <.0001
class Species;
Species Virginica 0 0.0000 0.0000 0.0000 0.0000 . .
model SepalLength=Species;
Scale 1 5.0962 0.2942 4.5509 5.7067
Posterior Intervals
Parameter Alpha Equal-Tail Interval HPD Interval
Intercept 0.050 64.4771 67.3561 64.4578 67.3284
SpeciesSetosa 0.050 -17.8639 -13.8305 -17.9082 -13.8751
SpeciesVersicolor 0.050 -8.5717 -4.5105 -8.6151 -4.5645
Dispersion 0.050 21.3747 33.7054 20.9688 33.1140
Step-by-step Example 2.2
ANOVA cont.
PROC GENMOD data=Iris;
class Species;
model SepalLength=Species;
bayes seed=1 coeffprior=uniform;
Step-by-step Example 2.3
Linear Regression
PROC GLIMMIX data=Liver;
model LC_nodes = BMI Age Time BCM AHBA Jaund / dist=Normal;
Time 10000 -0.0167 0.0277 -0.0352 -0.0168 0.00192 Jaund 0.050 -0.9916 2.9613 -1.0089 2.9212
Dispersion 0.050 24.6876 40.2912 24.1990 39.6035
BCM 10000 -1.5052 1.0603 -2.2129 -1.5099 -0.7910
Type III Tests of Fixed Effects Analysis Of Maximum Likelihood Parameter Estimates
Effect Num DF Den DF F Value Pr > F Standard Wald 95% Confidence Wald Chi- Pr > ChiS
BMI 1 129 0.29 0.5889 Parameter DF Estimate Error Limits Square q
Age 1 129 32.34 <.0001 Intercept 1 2.4508 0.2284 2.0032 2.8984 115.16 <.0001
Time 1 129 1.79 0.1833 BMI 1 -0.0044 0.0080 -0.0201 0.0114 0.29 0.5880
BCM 1 129 11.67 0.0008 Age 1 -0.0135 0.0024 -0.0181 -0.0088 32.34 <.0001
AHBA 1 129 14.94 0.0002 Time 1 -0.0029 0.0022 -0.0072 0.0014 1.79 0.1810
Jaund 1 129 6.31 0.0132 BCM 1 -0.2715 0.0795 -0.4272 -0.1157 11.67 0.0006
AHBA 1 0.3215 0.0832 0.1585 0.4845 14.94 0.0001
Jaund 1 0.2077 0.0827 0.0456 0.3698 6.31 0.0120
Scale 0 1.0000 0.0000 1.0000 1.0000
Step-by-step Example 2.4
Poisson Regression cont.
PROC GENMOD data=Liver;
model LC_nodes = BMI Age Time BCM AHBA Jaund / dist=Poisson link=log;
bayes seed=1 plots=none coeffprior=normal;
[14]
Step-by-step Example 3.2
ANOVA
Step-by-step Example 3.2
ANOVA cont.
*
Step-by-step Example 3.3
Simple Linear Regression
Step-by-step Example 3.3
Simple Linear Regression cont.
Assessment 2
https://und.qualtrics.com/jfe/form/SV_0ufeUVznLiqeZJc
Caveats and Concerns