Professional Documents
Culture Documents
Full download Bayesian Analysis with Excel and R 1st Edition Conrad Carlberg file pdf all chapter on 2024
Full download Bayesian Analysis with Excel and R 1st Edition Conrad Carlberg file pdf all chapter on 2024
https://ebookmass.com/product/financial-analysis-with-microsoft-
excel-8th-edition-timothy-r-mayes/
https://ebookmass.com/product/data-analysis-for-the-life-
sciences-with-r-1st-edition/
https://ebookmass.com/product/statistical-modeling-with-r-a-dual-
frequentist-and-bayesian-approach-for-life-scientists-pablo-
inchausti/
https://ebookmass.com/product/essentials-of-modern-business-
statistics-with-microsoft-excel-8th-edition-david-r-anderson/
Random Process Analysis With R Marco Bittelli
https://ebookmass.com/product/random-process-analysis-with-r-
marco-bittelli/
https://ebookmass.com/product/introduction-to-biostatistical-
applications-in-health-research-with-microsoft-office-excel-
and-r-2nd-edition-robert-p-hirsch/
https://ebookmass.com/product/statistics-for-ecologists-using-r-
and-excel-data-collection-exploration/
https://ebookmass.com/product/contentious-compliance-dissent-and-
repression-under-international-human-rights-law-courtenay-r-
conrad/
https://ebookmass.com/product/using-statistics-in-the-social-and-
health-sciences-with-spss-excel-1st/
About This eBook
ePUB is an open, industry-standard format for eBooks. However, support of ePUB and its many
features varies across reading devices and applications. Use your device or app settings to
customize the presentation to your liking. Settings that you can customize often include font, font
size, single or double column, landscape or portrait mode, and figures that you can click or tap to
enlarge. For additional information about the settings and features on your reading device or app,
visit the device manufacturer’s Web site.
Many titles include programming code or configuration examples. To optimize the presentation
of these elements, view the eBook in single-column, landscape mode and adjust the font size to
the smallest setting. In addition to presenting code and configurations in the reflowable text
format, we have included images of the code that mimic the presentation found in the print book;
therefore, where the reflowable format may compromise the presentation of the code listing, you
will see a “Click here to view code image” link. Click the link to view the print-fidelity code
image. To return to the previous page viewed, click the Back button on your device or app.
Bayesian Analysis with Excel and R
Conrad G. Carlberg
Contents at a Glance
Preface
Bayesian Analysis and R: An Overview
Generating Posterior Distributions with the Binomial Distribution
Understanding the Beta Distribution
Grid Approximation and the Beta Distribution
Grid Approximation with Multiple Parameters
Regression Using Bayesian Methods
Handling Nominal Variables
MCMC Sampling Methods
Appendix A Installation Instructions for RStan and the rethinking Package on the Windows
Platform
Glossary
Index
Excel Worksheets
To access bonus materials, please register your book at informit.com/register and enter ISBN
9780137580989.
Bayesian Analysis with Excel and R
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or
transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise,
without written permission from the publisher. No patent liability is assumed with respect to the
use of the information contained herein. Although every precaution has been taken in the
preparation of this book, the publisher and author assume no responsibility for errors or
omissions. Nor is any liability assumed for damages resulting from the use of the information
contained herein.
ISBN-13: 978-0-13-758098-9
ISBN-10: 0-13-758098-3
All rights reserved. This publication is protected by copyright, and permission must be obtained
from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or
likewise. For information regarding permissions, request forms and the appropriate contacts
within the Pearson Education Global Rights & Permissions Department, please visit
www.pearsoned.com/permissions/.
ScoutAutomatedPrintCode
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have been
appropriately capitalized. Pearson IT Certification cannot attest to the accuracy of this
information. Use of a term in this book should not be regarded as affecting the validity of any
trademark or service mark.
Microsoft and/or its respective suppliers make no representations about the suitability of the
information contained in the documents and related graphics published as part of the services for
any purpose. All such documents and related graphics are provided “as is” without warranty of
any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions
with regard to this information, including all warranties and conditions of merchantability,
whether express, implied or statutory, fitness for a particular purpose, title and non-infringement.
In no event shall Microsoft and/or its respective suppliers be liable for any special, indirect or
consequential damages or any damages whatsoever resulting from loss of use, data or profits,
whether in an action of contract, negligence or other tortious action, arising out of or in
connection with the use or performance of information available from the services.
The documents and related graphics contained herein could include technical inaccuracies or
typographical errors. Changes are periodically added to the information herein. Microsoft and/or
its respective suppliers may make improvements and/or changes in the product(s) and/or the
program(s) described herein at any time. Partial screenshots may be viewed in full within the
software version specified.
Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U.S.A.
and other countries. Screenshots and icons reprinted with permission from the Microsoft
Corporation. This book is not sponsored or endorsed by or affiliated with the Microsoft
Corporation.
Every effort has been made to make this book as complete and as accurate as possible, but no
warranty or fitness is implied. The information provided is on an “as is” basis. The author and
the publisher shall have neither liability nor responsibility to any person or entity with respect to
any loss or damages arising from the information contained in this book.
Special Sales
For information about buying this title in bulk quantities, or for special sales opportunities
(which may include electronic versions; custom cover designs; and content particular to your
business, training goals, marketing focus, or branding interests), please contact our corporate
sales department at corpsales@pearsoned.com or (800) 382-3419.
For questions about sales outside the U.S., please contact intlcs@pearson.com.
Editor-in-Chief
Mark Taub
Acquisitions Editor
Development Editor
Chris Zahn
Managing Editor
Sandra Schroeder
Tonya Simpson
Indexer
Timothy Wright
Proofreader
Donna E. Mulder
Technical Editors
Nick Cohron
Regina R. Monaco
Publishing Coordinator
Cindy Teeters
Cover Designer
Chuti Prasertsith
Compositor
CodeMantra
Credits
Figure 2-1, Figure 2-2, Figure 2-4, Figure 2-6, Figure 3-1 through Figure 3-13, Figure 4-1
through Figure 4-12, Figure 6-2 through Figure 6-4, Figure 7-1 through Figure 7-5, Figure 7-8,
Figure 8-1, Figure 8-3, Figure 8-4, Figure 8-6: Microsoft Corporation
Pearson is dedicated to creating bias-free content that reflects the diversity of all learners. We
embrace the many dimensions of diversity, including but not limited to race, ethnicity, gender,
socioeconomic status, ability, age, sexual orientation, and religious or political beliefs.
Education is a powerful force for equity and change in our world. It has the potential to deliver
opportunities that improve lives and enable economic mobility. As we work with authors to
create content for every product and service, we acknowledge our responsibility to demonstrate
inclusivity and incorporate diverse scholarship so that everyone can achieve their potential
through learning. As the world’s leading learning company, we have a duty to help drive change
and live up to our purpose to help more people create a better life for themselves and to create a
better world.
While we work hard to present unbiased content, we want to hear from you about any concerns
or needs with this Pearson product so that we can investigate and address them.
Preface
1 Bayesian Analysis and R: An Overview
Bayes Comes Back
About Structuring Priors
Watching the Jargon
Priors, Likelihoods, and Posteriors
The Prior
The Likelihood
Contrasting a Frequentist Analysis with a Bayesian
The Frequentist Approach
The Bayesian Approach
Summary
2 Generating Posterior Distributions with the Binomial Distribution
Understanding the Binomial Distribution
Understanding Some Related Functions
Working with R’s Binomial Functions
Using R’s dbinom Function
Using R’s pbinom Function
Using R’s qbinom Function
Using R’s rbinom Function
Grappling with the Math
Summary
3 Understanding the Beta Distribution
Establishing the Beta Distribution in Excel
Comparing the Beta Distribution with the Binomial Distribution
Decoding Excel’s Help Documentation for BETA.DIST
Replicating the Analysis in R
Understanding dbeta
Understanding pbeta
Understanding qbeta
About Confidence Intervals
Applying qbeta to Confidence Intervals
Applying BETA.INV to Confidence Intervals
Summary
4 Grid Approximation and the Beta Distribution
More on Grid Approximation
Setting the Prior
Using the Results of the Beta Function
Tracking the Shape and Location of the Distribution
Inventorying the Necessary Functions
Looking Behind the Curtains
Moving from the Underlying Formulas to the Functions
Comparing Built-in Functions with Underlying Formulas
Understanding Conjugate Priors
Summary
5 Grid Approximation with Multiple Parameters
Setting the Stage
Global Options
Local Variables
Specifying the Order of Execution
Normal Curves, Mu and Sigma
Visualizing the Arrays
Combining Mu and Sigma
Putting the Data Together
Calculating the Probabilities
Folding in the Prior
Inventorying the Results
Viewing the Results from Different Perspectives
Summary
6 Regression Using Bayesian Methods
Regression à la Bayes
Sample Regression Analysis
Matrix Algebra Methods
Understanding quap
Continuing the Code
A Full Example
Designing the Multiple Regression
Arranging a Bayesian Multiple Regression
Summary
7 Handling Nominal Variables
Using Dummy Coding
Supplying Text Labels in Place of Codes
Comparing Group Means
Summary
8 MCMC Sampling Methods
Quick Review of Bayesian Sampling
Grid Approximation
Quadratic Approximation
MCMC Gets Up To Speed
A Sample MCMC Analysis
ulam ’s Output
Validating the Results
Getting Trace Plot Charts
Summary and Concluding Thoughts
Appendix Installation Instructions for RStan and the rethinking Package on the Windows
Platform
Glossary
Index
Downloadable Bonus Content
Excel Worksheets
To access bonus materials, please register your book at informit.com/register and enter ISBN
9780137580989.
About the Author
Conrad Carlberg is a nationally recognized expert on quantitative analysis, data analysis, and
management applications such as Microsoft Excel, SAS, and Oracle. He holds a Ph.D. in
statistics from the University of Colorado and is a many-time recipient of Microsoft’s Excel
MVP designation. He is the author of many books, including Business Analysis with Microsoft
Excel, Fifth Edition, Statistical Analysis: Microsoft Excel 2016, Regression Analysis Microsoft
Excel, and R for Microsoft Excel Users.
Carlberg is a Southern California native. After college he moved to Colorado, where he worked
for a succession of startups and attended graduate school. He spent two years in the Middle East,
teaching computer science and dodging surly camels. After finishing graduate school, Carlberg
worked at US West (a Baby Bell) in product management and at Motorola.
This book has several aspects that I want to let you know about up front. If you’re already
comfortable with terminology and concepts such as Hamiltonian Monte Carlo sampling,
conjugate pairs, and posterior distributions, then this book is probably not for you. You already
know a lot about those topics, and if you need more you know where to find it.
On the other hand, if you don’t feel quite at home with the purpose of random samples, R’s user
interface, and why you might want to work with mean-corrected instead of with raw values, then
it’s just possible that this book offers something that you might want to know about. Both this
book and I assume that you have some background in statistical analysis—say, at the
introductory college level, where you can expect to study some probability theory and how it
applies to the assessment of sample means, variances, and correlations. Particularly if you have
studied these problems in the past, you will be better placed to understand how Bayesian analysis
differs from traditional approaches, and how it works out in the context of the functions and
packages found in R. And if you feel as though you could use some refresher work in traditional
statistical analysis, Pearson is making available to you for download an e-book titled Statistical
Analysis: Microsoft Excel 2016. You’ll find details on obtaining that book at the end of this
Preface.
You’re experienced. You probably have something close to the background in Bayesian analysis
that I had in mind when I laid out the topics that I wanted this book to cover. It seemed to me
that the world already has plenty of books about statistics and experimental methodology: one
more isn’t going to help much. Something similar can be said about using syntax and diction that
R recognizes: we already have as many elementary to intermediate texts on R as we need.
What we did need, I thought, was a source of information that connected the simplistic
capabilities of VBA (the programming language historically offered by Microsoft Excel to give
the user more control over the application) with the more sophisticated capabilities of
programming languages such as R and C.
Similarly, we were missing information about three basic types of sampling that range from the
simplistic, univariate sort of categorical analysis that you find in undergraduate texts to the
complex sampling methods used by techniques such as quadratic approximation and Markov
Chain Monte Carlo (MCMC). Richard McElreath has written, and has supplied to R, helper
functions that ease the task of designing, writing, and installing the code that does the heavy
lifting for you.
I have done what I can in this book to leverage the Excel skills that you have already developed
in the areas of managing functions, handling data, and designing graphs and plots. The point will
come that you see that Excel too handles the necessary tools of calculus in the form of function
arguments—albeit more slowly and awkwardly. Shortly thereafter you’ll see how the three
fundamental approaches to building posterior distributions by sampling are in fact wonderfully
creative solutions to the same problem.
When I first approached Pearson about writing this book, I came away from the discussions just
a little discouraged. The editors and their advisors were polite and really good at listening, but I
didn’t think that I heard much in the way of encouragement. In particular, they wanted to know
why I would want to write this book.
Good question. I had several reasons in mind, but it wasn’t easy to articulate them. Still, I did so,
and apparently I did so successfully because, well, look at what you’re holding. And those
reasons made sense as a place to start out, but I’ll keep it to the first two that occurred to me:
Why would you want to read it? There are several reasons, but if you are like most of us you
use Microsoft Excel for most numeric purposes, even though Excel was designed as a
general-purpose calculation engine. You might have stayed away from Bayesian analysis
because you heard that Excel is comparatively slow. And you’re right: because of both
software problems and hardware issues, there was a time when you had to wait and wait for a
solution to the problem that you posed to Bayesian software. No longer. Now you can get an
answer in a reasonable length of time, and without making assumptions that you don’t feel
quite comfortable with.
People I work with were using familiar words in unfamiliar ways. They were using terms like
prior, likelihood, and parameter in contexts that they did not seem to fit. I wanted to find out
more about what they were saying. But I needed a starting point, and because I was quite
familiar with Excel’s numeric capabilities, I decided to work from the platform of Excel and
toward a platform based on R. It’s true that Excel is comparatively slow and doesn’t have
many functions that you would like to have in a Bayesian-oriented platform. But for certain
problems, Excel works great and returns accurate results in a short timeframe. Fine; I can
work from there.
The basic idea behind a Bayesian analysis is to create a posterior distribution that informs you
about the parameters that bring about the results of the simulation. You do not want to start a
sequence with one family of distributions and then try to finish the sequence in another family,
so you should aim for a situation in which the prior and the likelihood are from the same family.
That, of course, implies that you select the distributional family from which the product will
stem. You have several families from which to choose, but your choice will almost inevitably
depend on the specific questions that you want to answer, which in turn depend on the nature of
the data that you want to analyze.
One basic family of distributions is the binomial distribution. The term binomial itself implies
the nature of a binomial distribution: two names, such as win and loss, buys and doesn’t buy,
survives and fails to survive, and so on. Consider your lifetime experience with coins. You have
almost surely come to expect that when you pull a coin at random from your pocket and flip it,
the probability is 50% that it will come up heads and 50% that it will come up tails. That’s a
binomial distribution: two names, two outcomes, two results.
The distinctive feature of a binomial distribution is that its values are discrete rather than
continuous. When you flip the coin, you do not anticipate that the flip could come up with any of
an infinite number of results. You anticipate two and only two outcomes, heads and tails.
This can be a very different situation from that of a person’s height or weight. Then, each
measurement is just one of an infinite number of possible heights or weights. The beta
distribution, discussed in Chapter 3, is an example of a continuous distribution as distinct from a
discrete one, such as the binomial. When you set up your analysis using R, for example, you can
specify that a given parameter should be distributed as binomial, or any of R’s distributional
families. This flexibility is one characteristic that makes R’s structure, and its design, so useful in
Bayesian analysis.
Right here’s a good spot to stress that it’s important to specify the distributional characteristics of
the parameters you use in an analysis, but don’t let them blind you to other aspects—aspects that
you might well ignore if you were to ignore all the good reasons for adding Bayes to your
toolkit.
It’s all too easy to forget that one of the key assumptions underlying a binomial test is that any
two tests in your experiment are independent of one another. Suppose that you are studying the
distribution of political party membership; one of the questions you ask is therefore which party,
if any, a respondent belongs to.
To make a valid inference regarding the probability of a participant’s response, you must be sure
that the response is independent of any other response in your survey. So, the value of George’s
response must be unaffected by the value of Ellen’s response. If that is the case, you’re able to
add and subtract subtotals directly (for example, to derive cumulative totals) without having to
adjust for some probably unknowable dependency in the data.
The history of the world has never exhibited more exalted devotion
to an idea, nor a more splendid patriotism than that of the people of
East Tennessee. We may almost challenge the records of religious
history to produce anything more like holy enthusiasm, than the lofty
inspiration which has characterized these people. In no country, and
among no class can be found more heroic persistence or unfaltering
adherence to principle than has exalted the patriotism of this region.
With many inhabitants of the eastern portion of the State, loyalty
and devotion to the Union became in truth a part of their religion.
The rebel leaders knew that they had very little sympathy in East
Tennessee, and took measures to crush out all Union sentiment with
the iron heel of military despotism. Any expression of sympathy with
the Union cause, any co-operation of its inhabitants with the
loyalists, either for their own protection or for the aid of the
Government, was punished as a crime. The presses of that part of the
State had all been silenced or converted to their own use by the
Secessionists, with one exception. The Knoxville Whig remained true
to the Union. Its vigorous defence of the Government, its exposures
and denunciations of the rebel leaders, its unsparing invective
against the rebellion, and its bold, defiant appeals to the people, rang
like a clarion through the hills and valleys of East Tennessee, and as
the echo gathered from thousands of loyal voices, it made itself heard
through all the valleys and mountain passes of that noble border
State.
The heroic editor of this paper was not to be silenced either in his
voice or his press without a vigorous struggle. The Rev. Wm. G.
Brownlow had learned how to denounce and how to endure, for that
is a lesson most Methodist clergymen are called upon to learn; and
being brave in deeds as well as words, he stood forth in defence of
the country he loved, when she greatly needed the power of his
eloquence and the strength of his arm. The popularity which this
man had won by his uprightness, his courage, and firm adherence to
the Constitution, gave his opinions a force that made him an object
of peculiar importance to the enemy—yet they hesitated to lay violent
hands upon a man whose words were more potent than their
bayonets.
He was frequently threatened by soldiers passing through
Knoxville from other States, yet none dared to execute their threats.
His family were inspired with the same lofty heroism, and on one
occasion when a company of rebels came to his house to haul down
the Stars and Stripes, which was kept floating over his domicil, one
of his daughters stepped out to meet them, and by her courage and
decision protected the flag.
The suppression of this undaunted advocate of the Union, and
faithful and fearless witness against secession, became an inevitable
necessity; and at last, in the hope that he would at least become
silent on political affairs, it was resolved to offer him the alternative
of the oath of allegiance or the cell of a prison. He chose the latter,
and in a valedictory to his readers, published October 26, which must
ever be memorable for its heroic defence of the Union, its bold
denunciation of the rebels and their course, he announced to his
readers the suspension of his paper. This remarkable address, which,
under the circumstances, rises to the sublime in its moral courage,
closed with these words:
“Exchanging, with proud satisfaction, the editorial chair and the sweet
endearments of home for a cell in the prison, or the lot of an exile, I have the honor
to be, &c.
William G. Brownlow.”
Mr. Brownlow was sent to prison, and for months occupied a room
with several other patriots who preferred imprisonment to denial of
the government they loved. Here he was in daily expectation of being
led forth to execution. Though suffering from ill-health he was no
way daunted by the dark fate that threatened him. Nor were these
anticipations groundless, for during his stay there, many a brave
man left that prison to meet a violent death, and he had no reason to
expect a happier destiny.
During the closing months of the summer and fall the hopes of the
people were excited by promises of aid from the government. Loud
and earnest appeals were made for help, and with the energy of
despair the people clung to their principles, through every species of
persecution, robbery, arson, and imprisonment. Hundreds were
hung or assassinated, and the records of Tennessee are among the
most heart-rending that this war for the Union will leave to posterity.
The position of the rebel armies in western Tennessee was at that
time very strong, but the importance of keeping their lines of
communication open with the Atlantic States was great, and
thoroughly understood by the loyalists. To cut these lines was to the
Federals a work of pressing necessity; and in view of the probable
redemption of East Tennessee, the loyalists organized, and on the
night of November 8 they destroyed several bridges, and broke the
lines. Two of these were on the Georgia State road, two on
Chickamanye Creek, Hamilton county, and one on the East
Tennessee and Georgia railroad, on Hiawassee river, Bradley county.
Besides these, two bridges on the East Tennessee and Georgia
railroad on Lick Creek, Green County, and another on Holstein river,
were also burned. The rebels were thrown into consternation by
these events, and their leaders took the most active measures to
arrest and punish the perpetrators. A correspondence between some
of the prominent men ensued, and a large portion of the letters was
discovered among other papers and effects captured after the battle
of Mill Spring, which took place on the 19th of January, 1862. This
correspondence, in which the names Colonel William B. Wood and
General F. K. Zollicoffer appear, prove that the majority of the people
were unalterably for the Union, and that they could only be
restrained by the most oppressive and cruel measures. Colonel Wood
wrote to J. P. Benjamin, the Secretary of War, asking what
disposition should be made of the bridge-burners, to which Mr.
Benjamin replied—“All such as can be identified as having been
engaged in bridge-burning are to be tried summarily by drumhead
court-martial, and if found guilty, executed on the spot by hanging. It
would be well to leave their bodies hanging in the vicinity of the
burnt bridges.”
The loyalists were encouraged in their cause by the devotion of
Hon. Andrew Johnson, U. S. Senator, and Hon. Horace Maynard, M.
C., for Tennessee, whose eloquent and powerful appeals, and
confident assurances of aid, cheered the hearts of the people.
Thousands of East Tennesseeans escaped by night, wandering
along unfrequented roads, until they reached Kentucky, where they
organized regiments, under the direction of the Federal
commanders. Their cherished desire was to return to their own State,
with a powerful army, and redeem their soil. The atrocity of the rebel
guerrillas drove them almost to a passion of revenge, and when
disappointed at the announcement that their time had not come, and
that they must await a more favorable condition of the army,
hundreds of them, when ordered to retreat from the border lines of
their State, strayed from the ranks, despairing and heart-sick, and
falling down by the way, wept bitterly. Several of them, exhausted by
hard labor and forced marches, never rose again, but were
afterwards found dead on the road to Mount Vernon.
On the 26th of November the house of a gentleman named Bell
was attacked by an armed party of the enemy and set on fire. The
inmates, a large family of nine persons, were consigned to the
flames. Two alone of the whole household escaped this horrible fate.
On the 29th a band of twenty-one Union prisoners at Nashville
were compelled to take the oath of allegiance, and enter a company
in the rebel army.
Leadbetter, the secession commander in East Tennessee, had his
headquarters at Greenville, and on the 30th of November issued a
proclamation promising protection and pardon to all who would lay
down their arms and submit to the Confederate government. From
this clemency he excepted bridge-burners and destroyers of railroad
tracks. He closed his proclamation with the assurance that “they will
be tried by drumhead court-martial, and be hung on the spot.” This
terrible order was put into execution a few days afterward. Jacob M.
Hemslier and Henry Fry, two Unionists, being tried and pronounced
guilty of these offences, were hung.
The days of hope for the Unionists were weary and prolonged, but
deliverance was drawing nigh. The loyal men of the western part of
the State organized to oppose the measures of the leaders, and early
in January a bold resistance was made in Carroll, Weakly, McNairy,
and other counties, against the conscription act. Rebel troops were
sent into these counties to compel submission, and enforce
obedience.
The defeat and death of Zollicoffer, the breaking up of his army,
and the destruction of his stronghold, at last gave a brilliant promise
to these persecuted people that their deliverance was drawing nigh.
This event, succeeded in a few weeks by the capture of Fort Henry,
Fort Donelson, the evacuation of Bowling Green and Columbus, and
the occupation of Nashville, filled every true heart with rejoicing, and
the good old flag once more swept its folds freely over the houses of
East Tennessee.
February 6, 1862.