Instant download (Ebook) Advanced R Statistical Programming and Data Models: Analysis, Machine Learning, and Visualization by Matt Wiley, Joshua F. Wiley ISBN 9781484228715, 1484228715 pdf all chapter
Instant download (Ebook) Advanced R Statistical Programming and Data Models: Analysis, Machine Learning, and Visualization by Matt Wiley, Joshua F. Wiley ISBN 9781484228715, 1484228715 pdf all chapter
com
DOWLOAD EBOOK
ebooknice.com
ebooknice.com
ebooknice.com
https://ebooknice.com/product/sat-ii-success-
math-1c-and-2c-2002-peterson-s-sat-ii-success-1722018
ebooknice.com
(Ebook) Matematik 5000+ Kurs 2c Lärobok by Lena
Alfredsson, Hans Heikne, Sanna Bodemyr ISBN 9789127456600,
9127456609
https://ebooknice.com/product/matematik-5000-kurs-2c-larobok-23848312
ebooknice.com
ebooknice.com
ebooknice.com
https://ebooknice.com/product/beginning-r-4-from-beginner-to-
pro-12077986
ebooknice.com
https://ebooknice.com/product/beginning-r-4-from-beginner-to-
pro-23347418
ebooknice.com
Matt Wiley and Joshua F. Wiley
Joshua F. Wiley
Columbia City, IN, USA
Trademarked names, logos, and images may appear in this book. Rather
than use a trademark symbol with every occurrence of a trademarked
name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark. The use in this publication
of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of
opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true
and accurate at the date of publication, neither the authors nor the
editors nor the publisher can accept any legal responsibility for any
errors or omissions that may be made. The publisher makes no
warranty, express or implied, with respect to the material contained
herein.
Conventions
Bold lowercase letters are used to refer to a vector, for example, x . Bold
uppercase letters are used to refer to a matrix, for example, X .
Generally, the Latin alphabet is used for data and the Greek alphabet is
used for parameters. Mathematical functions are indicated with
parentheses, for example, f (·).
In the text, reference to R code or function will be in monospaced
font like this. R function names have parentheses included to
help indicate it is a function, such as mean() to indicate the mean
function in R .
Package Setup
Throughout the book, we will make use of many different R packages
that make tasks easier or provide more robust or sophisticated
graphing and analysis options.
Although not required for readers, we make use of the
checkpoint package to help ensure the book is reproducible [23]. If
you do not care about reproducibility and are happy to take your
chances that our code that worked with one version of R and packages
also works with whatever versions you have, then you can just skip
reading this section. If you want reproducibility, but do not care why or
how it works, then just create R scripts for the code for each chapter,
save them, and then run the checkpoint package at the beginning. If
you care and want to know why and how it all works, read on the next
few paragraphs.
Details on Reproducibility
The many additional packages available for R are one of its greatest
strengths. However, they also create some challenges. For example, as a
reader, suppose that on your computer, you have R v3.4.3 installed
and as part of that in January you had installed the ggplot2 package
for graphs. By default, you will have whatever version of ggplot2 was
available in January when you installed it. Now in one chapter, we tell
you that you need both the ggplot2 and cowplot packages. Because
you already had ggplot2 installed, you do not need to install it again.
However, suppose that you did not have the cowplot package
installed. So, whenever you happen to be reading that chapter, you
attempt to install the cowplot package, let’s say it’s in April. You will
now by default get the latest version of cowplot available for that
version of R as of April.
Now imagine a second reader comes along and also had R v3.4.3
but had neither the ggplot2 nor the cowplot package installed. They
also read the chapter in April, but they install both packages in April, so
they get the latest version of both packages available in April for R
v3.4.3 .
Even though both you and the other reader had the same version of
R installed, you will end up with different package versions from each
other, and likely different versions yet from whatever versions we used
to write the book.
The end result is that different people, even with the same version
of R, very likely are using different versions of different packages. This
can pose a major challenge for reproducibility. If you are reading a
book, it can be a frustration because code does not seem to work as we
said it would. If you are using code in production or for scientific
research or decision-making, nonreproducibility can pose an even
bigger challenge.
The solution to standardize versions across people and ensure
results are fully reproducible is to control not only the version of R but
also the version of all packages. This requires a different approach to
package installation and management than the default system, which
uses the latest package versions from CRAN. The checkpoint
package is designed to solve this challenge. It does require some extra
steps and processes to use, and at first may seem a nuisance, but the
payoff is that you can be guaranteed that you are not only using the
same version of R but also the same version of all packages.
To understand how the checkpoint package works, we need a bit
more background regarding how R ’s libraries and package system
work.
Mainstream R packages are distributed through CRAN. Package
authors can submit new versions of their packages to CRAN, and CRAN
updates nightly. For some operating systems, CRAN just stores the
package source code, such as for Linux machines. For others, such as
Windows operating systems, CRAN builds precompiled package
binaries and hosts those. CRAN keeps old source code but generally not
old binary packages for long. On a local machine, when
install.packages is run, R goes online to a repository, by default
CRAN, finds the package name, downloads it, and installs it into a local
library . The local library is basically just a directory on your own
machine. R has a default location it likes to use as its local library, and
by default when you install packages, they are added to the default
library. Once a package is installed, when it is loaded or opened using
library(), R goes to its default library, finds a package with the
same name, and opens it.
The checkpoint package works by creating a new library on the
local machine, for a particular version of R for a particular date. Then it
scans all the R script files in R ’s current working directory—you can
identify this using the getwd() function—and identifies any calls to
the library() or require() functions. Then it goes and checks
whether those packages are installed in the local library. If they are not,
it goes to a snapshot of CRAN taken by another server setup to support
the checkpoint package. That way, checkpoint can install the
version of the package available from a specific date. In that way, the
checkpoint package can ensure that you have the same specific
version of R and specific version of all packages that we used when
writing the book. Or if you are trying to re-run some analysis from a
year ago, you can get the same version of those packages on a new
computer.
Assuming that you have the following code in an R script, you can
use the checkpoint package to read the R script and find the call to
library(data.table), and it will install the data.table
package, which is a great package for data management [29]. If you do
not want checkpoint to look in the current working directory, you
can specify the project path, as we do to the book in this example. You
can also change where checkpoint sets its library to another folder
location, instead of the default location, which we also do. We
accomplish both of these using variables set as part of our R project,
book_directory and checkpoint_directory . If you are using
checkpoint on your own machine, set those variables to the relevant
directories, for example, as book_directory <-
"path/to/your/directory" . Note that whatever folder you
choose, R will need read and write privileges for that folder.
library(checkpoint)
checkpoint("2018-09-28", R. version = "3.5.1",
project = book_directory,
checkpointLocation = checkpoint_directory,
scanForPackages = FALSE,
scan.rnw.with.knitr = TRUE, use.knitr = TRUE)
library(data.table)
options(
width = 70,
stringsAsFactors = FALSE,
digits = 2)
Data Setup
One of the datasets we will use throughout this book is a longitudinal
study, the Americans’ Changing Lives (ACL) [45]. This is publicly
available data and can be downloaded by going to
http://doi.org/10.3886/ICPSR04690.v7 .
The Americans’ Changing Lives (ACL) is a longitudinal study with
five waves of data, shown in Table I-1 .
Wave Year
W1 1986
W2 1989
W3 1994
W4 2002
W5 2011
load ("../ICPSR_04690/DS0001/04690-0001-
Data.rda")
ls ()
## [1]
"book_directory" "checkpoint_directory"
## [3]
"da04690.0001" "render_apress"
setnames(acl, names(acl), c(
"ID", "Sex", "RaceEthnicity", "SESCategory",
"Employment_W1", "BMI_W1", "Smoke_W1",
"PhysActCat_W1",
"AGE_W1",
"SWL_W1", "InformalSI_W1", "FormalSI_W1",
"SelfEsteem_W1", "Mastery_W1",
"SelfEfficacy_W1",
"CESD11_W1", "NChronic12_W1",
"Employment_W2", "BMI_W2", "Smoke_W2",
"PhysActCat_W2",
"InformalSI_W2", "FormalSI_W2",
"SelfEsteem_W2", "Mastery_W2",
"SelfEfficacy_W2",
"CESD11_W2", "NChronic12_W2"
))
acl[, ID := factor(ID)]
acl[, SESCategory := factor(SESCategory)]
acl[, SWL_W1 := SWL_W1 * -1]
Joshua F. Wiley
is a lecturer in the Monash Institute of Cognitive and Clinical
Neurosciences and School of Psychological Sciences at Monash
University. He earned his PhD from the University of California, Los
Angeles, and completed his postdoctoral training in primary care and
prevention. His research uses advanced quantitative methods to
understand the dynamics between
psychosocial factors, sleep, and other
health behaviors in relation to
psychological and physical health. He
develops or codevelops a number of R
packages including varian , a package
to conduct Bayesian scale-location
structural equation models, and
MplusAutomation , a popular package
that links R to the commercial Mplus
software, and miscellaneous functions to
explore data or speed up analysis in
JWileymisc .
library(checkpoint)
checkpoint("2018-09-28", R.version = "3.5.1",
project = book_directory,
checkpointLocation = checkpoint_directory,
scanForPackages = FALSE,
scan.rnw.with.knitr = TRUE, use.knitr = TRUE)
library(knitr)
library(ggplot2)
library(cowplot)
library(MASS)
library(JWileymisc)
library(data.table)
1.1 Distribution
Visualizing the Observed Distribution
Many statistical models require that the distribution of a variable be
specified. Histograms use bars to graph a distribution and are probably
the most common graph used to visualize the distribution of a single
variable. Although relatively rare, stacked dot plots are another
approach and provide a precise way to visualize the distribution of data
that shows the individual data points. Finally, density plots are also
quite common and are graphed by using a line that shows the
approximate density or amount of data falling at any given value.
ggplot(mtcars, aes(mpg)) +
geom_dotplot()
Figure 1-1 Stacked dot plot of miles per gallon from old cars
As a brief aside, much of the code for ggplot2 follows the format
shown in the following code snippet. In our case, we wanted a dot plot,
so the geometric object, or “geom”, is a dot plot (geom_dotplot() ).
Many excellent online tutorials and books exist to learn how to use the
ggplot2 package for graphs, so we will not provide a greater
introduction to ggplot2 here. In particular, Hadley Wickham, who
develops ggplot2, has a recently updated book on the package,
ggplot2: Elegant Graphics for Data Analysis [109], which is an excellent
guide. For those who prefer less conceptual background and more of a
cookbook, we recommend the R Graphics Cookbook by Winston Chang
[20].
ggplot(the-data, aes(variable-to-plot)) +
geom_type-of-graph()
Unlike a dot plot that plots the raw data, a histogram is a bar graph
where the height of the bar is the count of the number of values falling
within the range specified by the width of the bar. You can vary the
width of bars to control how many nearby values are aggregated and
counted in one bar. Narrower bars aggregate fewer data points and
provide a more granular view. Wider bars aggregate more and provide
a broader view. A histogram showing the distribution of sepal lengths
of flowers from the famous iris dataset is shown in Figure 1-2.
ggplot(iris, aes(Sepal.Length)) +
geom_histogram()
ggplot(data.table(lynx = as.vector(lynx)),
aes(lynx)) +
geom_histogram()
ggplot(data.table(lynx = as.vector(lynx)),
aes(log(lynx))) +
geom_histogram()
Density Plots
Another common tool to visualize the observed distribution of data is
by plotting the empirical density. The code for ggplot2 is identical to
that for histograms except that geom_histogram() is replaced with
geom_density() . The code follows and the result is shown in Figure
1-5.
ggplot(iris, aes(Sepal.Length)) +
geom_density()
Figure 1-5 This is the density plot for our sepal lengths
Empirical density plots include some degree of smoothing, because
with continuous variables, there is never going to be many observations
at any specific value (e.g., it may be that no observation has a value of
3.286, even though there are values of 3.281 and 3.292). Empirical
density plots show the overall shape of the distribution by applying
some degree of smoothing. At times it can be helpful to adjust the
degree of smooth to see a coarser (closer to the raw data) or smoother
(closer to the “distribution”) graph. Smoothing is controlled in
ggplot2 using the adjust argument. The default, which we saw in
Figure 1-5, is adjust = 1. Values less than 1 are “noisier” or have less
smoothing, while values greater than 1 increase the smoothness. We
compare and contrast noisier in Figure 1-6 vs. very smooth in Figure 1-
7.
ggplot(iris, aes(Sepal.Length)) +
geom_density(adjust = .5)
ggplot(iris, aes(Sepal.Length)) +
geom_density(adjust = 5)
By Edwin Hodder,
Author of “Memories of New Zealand Life;” “Junior Clerk;” “Tossed on the Waves,”
&c.
Now I confess I never had a passion for street music, but this was
much beyond the average of merit. It was a part of my education to
love and venerate old customs, and it was a part of my creed always
at Christmas time to relieve, as far as lay in my power, those who
were excluded from the privileges which are enjoyed by those who
have been more highly favoured by a kind providence. So, putting
on my hat, I went to the door with a jug of foaming ale and a glass,
and Andrew followed me with a good many shining pieces of silver
in his hand, which he had collected in the room. As soon as I
opened the door, an old man, with a lantern in one hand and his hat
in the other, stood before us to receive the contribution. Just as
Andrew was putting the money into his hat the light flashed up into
his face. A spasm of joy and fear shot through me; I staggered back,
and should have fallen, had not Andrew held me. The old man was
my father! Wrinkled as the face was, white as the hair had grown,
bent as was that once graceful figure, I was absolutely certain that I
was not mistaken. In a moment my self-possession returned, and in
that moment I realized the meaning of the phrase, “quick as
thought.” For I remembered that I was only a boy when my father
went away, and he could not recognise me. I remembered that a
sudden shock of joy might deprive me of my mother, the very while
it restored to me my father. I understood that Andrew had
ascertained the meaning of my sudden emotion, for he had adroitly
screened me from the gaze of my father and the minstrels, and was
beginning to pour out the ale for them. I thought, too, that a sudden
revelation of myself to my father might be injurious to him; and not
knowing the state of his mind, it might be the most fatal thing to
surprise him. So at once my plans were made; and all this happened
in a moment! I whispered to Andrew, “Be spokesman, for my voice
may betray me. Invite them to supper at the ‘King’s Arms’ in an
hour, on condition that they will sing some carols. Say they shall be
well paid. Follow them till then, and don’t let your eyes be off my
father for an instant. I will gently prepare them indoors.”
“All right,” said Andrew, “but come inside with me first, while I get
my coat, and tell them I am going. I will not damage your plan.”
There was great surprise when Andrew called for his coat, and
said to his wife, “Nelly, my dear, I must leave you and our good
friends for a little while. I know you will pardon me, but I have just
seen an old friend who I knew in my school days, and he seems in
distress. I can’t ask him in here to-night, but I will see him into the
hotel at the end of the street; and, John, come for me in half-an-
hour to release me, if I am detained so long.”
Without waiting longer than to give Nelly a kiss he was off, and
the street door closed upon him. Then began an attack of questions
which puzzled my ingenuity to parry. There was something
remarkably strange in the event, and their curiosity was strongly
excited. There was not much time to lose, and the questions were
working me towards the subject. At last Nelly said, “Cannot you
guess, John, at all, who this stranger is, or what he wants with
Andrew.” And then I said, “I did not mean to tell you, or to awaken
sad memories, especially to-night, but I think the stranger will be
able to tell him something of the fate of father!”
I had said enough and seen enough to know that I might safely
carry out my plan. Affected as my mother was by the news, she was
perfectly calm. She did not weep, or dream of fainting, or going into
hysterics, but a holy joy lighted up her face, and her very smile was
a thanksgiving. Brave little Nell clasped her hands together (it was
just the attitude she used on that other Christmas Eve), and said,
“Thank God.” For a minute or two there was a dead silence in the
room, only broken by Captain Wray, who took snuff violently.
“John,” said my mother, at length, and her voice faltered just a
very little, “John, you know more than you have told; let me hear it
all. I am more than strong enough to bear it; I have waited for years
in preparation of this hour. Tell me, when did it happen, and where?”
I sat down between her and Nelly, and said, as calmly as I could,
“Now, my dearest mother, be brave and cheery, father is still alive. It
would not be well for you to see him for some time, but he is in
London, and well.”
The tears came at last; not a dry eye was in the room; but when I
left them to go with Captain Wray to the “King’s Arms” (for he could
not remain inactive), a voice had said to the storm of feeling,
“Peace, be still;” and there was a great calm.
My story is nearly ended. That night I made myself known to my
father, and the shock of feeling at seeing me and learning that my
mother and sister were alive and near him, instead of doing him
injury, effected a good that probably nothing else could have done.
His was a strange wild history, and it was only little by little, and that
extending over a long time, as the powers of mind and memory
gradually returned, that I learnt it. When he left his home it was
under the terrible delusion that nothing but the workhouse was
before him, and he could not bear to see the distress that would
come upon his family. He took ship to America, and on the voyage
his mind gave way. Arrived in that country, he was placed in proper
care by the authorities, but in all the wanderings of his mind he
never divulged his name or residence. Several times his reason
became temporarily restored, but then the thought of his deserted
wife and home was too terrible for him ever to think resolutely of
returning thither. Years passed in this way, and during his rational
periods he had to earn his bread by the sweat of his brow. At last he
so far recovered that his determination to return to his native land,
and at least ascertain what had become of his family, was carried
into effect. Penniless when he arrived, and the season cold and
inclement, he had to endure severe hardships. Circumstances
brought him into the company of a band of carol singers, to whom
he engaged himself as money collector, and he had resolved to work
his way to Marantby as soon as he was able.
The best medical advice that could be had in London was obtained
for him, and, by the blessing of God, his health of body and mind
was restored. I will not attempt to describe the meeting with my
mother, for no eye saw it. The effect was not injurious, on the
contrary, from that day his old habits and spirits began to return,
and for many years his life was one of unmingled peace and
happiness.
And so it came to pass that Christmas Day ceased to be a day of
painful memories, for we could say, “He was dead and is alive again,
and was lost and is found.” Now his body rests beside that of my
mother in the little churchyard at Marantby, and their spirits are in
the bright world, where, perhaps, the angels are singing again this
night that beautiful song they sang years ago, “Glory to God in the
highest, and on earth peace and goodwill towards men.”
CASTLE CONNOR.
By Mona B. Bickerstaffe,
Author of “Begin Well, End Well;” “Araki, the Damio,” &c.
Christmas was always a merry season at Castle Connor, and not less
merry than usual was the Christmas time of which I am going to tell
you. The house was as full as it could hold, and added to the usual
number of guests were three English cousins, who had come over to
pay their first visit to their Irish relatives. At the public school from
which they came, they were known as Max, Major, and Minor, and so
we shall name them here. Max (whose real name was Dick Lindsaye)
was in his seventeenth year, and only famous at K⸺ for being the
biggest dunce, the biggest bully, and the biggest boaster in the
school; for, while careful to avoid every kind of danger, he was prone
to forge Falstaffian tales of the dangers he had surmounted, when
no one was there to see him. Tom and Harold Cunliffe were his step-
brothers; the former was a soft-faced boy, about thirteen years of
age, with curly, brown hair, dark brown eyes, and a countenance
beaming with good nature and good temper, but evidently a being
more capable of enjoying the dolce far niente than any state of life
in which he might be expected to be an active or energetic member
of society. Yes, a quiet, easy-going youth was Tom, very different
from the twelve-year-old Harry, a wiry, springy young fellow, who,
while living in great awe of his big brother, was always laying plots
for fun at his expense.
Major and Minor were great favourites wherever they went, while
no one could endure Max’s snobbish conceit and self-importance. To
have a speck of mud upon his highly polished boots, or a grain of
dust upon his ever-glossy clothes, was to spoil his pleasure for the
day, while his young brothers, unfortunately, went off to the
opposite extreme, and were only too regardless of their personal
appearance.
The family at Castle Connor consisted of the father, mother, two
daughters, and three sons. The latter were manly, warm-hearted
youths, quick-tempered and quick-witted, first rate horsemen,
masters of all field sports, but not very polished in their manners.
They had never been to any school, but were brought up at home,
under the care of a tutor, whom they managed just as they pleased,
and who found himself in too snug a berth at Castle Connor, to
venture to make it less agreeable by complaints that his pupils too
often preferred sporting to Latin. He (Mr. Moriarty) was at Christmas
time always absent, enjoying the holidays at his own home, so the
youths were then left entirely to their own devices, which generally
led them to play tricks of all kinds upon the rest of the household.
Directly they saw Max, they (as Major said) “twigged him at once,”
and came to the conclusion that he was fair game for fun. He soon
adopted a patronizing manner to Dennis Connor, most aggravating
to that high-spirited youth, who cast about in his very fertile mind as
to how he might, once for all, humble the self-conceit of his lofty
cousin; and finding that his father was to be away from home for a
day or two, he laid his plans accordingly. They were standing
together in the drawing-room, waiting for dinner, when he turned
abruptly to Max, and asked him if he had seen the dungeons of
Castle Connor?
“The dungeons! no. I never knew there were any.”
Mrs. Connor did not appear to know it either, for she looked up in
astonishment, but a look from her son silenced her.
“Don’t say anything now,” said Dennis, mysteriously; “I’ll tell you
all about it after dinner.”
After dinner, accordingly, Dennis took him aside, and told him that
he never mentioned the dungeons before his father or mother, for
“fact is,” said he, “they are a great source of annoyance and
discomfort to them, and all of us. Dark deeds have been done down
there, in the times of the fights between the O’Connors and the
Condons. Weird sounds come from them in the night time, especially
on certain nights, when one Con Condon, the headless, is said to
come to look for his head, which centuries ago was taken from him
by our ancestor, Modha O’Connor, who married a daughter of Oilioll
Olum,[3] King of Munster. Oilioll had carried off a beautiful lady,
Modweena Condon, but somehow she contrived to escape from the
place where he had locked her up, and seeing him sleeping, she in
revenge bit off his ear while he slept, whereon Oilioll, roused by the
pain, seized a spear, and thrust it through her with such force that
he flattened the point against a stone in the wall. Drawing forth the
spear, regardless of his victim’s agonies, he tried to straighten the
point with his teeth, but it had been poisoned, and from that
moment his teeth became jet black. I only tell you this in case you
should notice the dark colour of father’s teeth, for the blackness still
runs in the family; but, as I was saying, Modha O’Connor took the
Condon prisoner, brought him to his castle here, shut him up in the
dungeon, and coolly cut off his head. When the clan heard of it, they
assembled in great force, stormed the castle, broke into the
dungeons, and found the body of their chief, but nowhere could they
find his head. The body was buried with all funeral honours, when
the earth fell over it, wild unearthly voices sang the Dahtan Da mort,
Augustha Cadine; but the spirit of Con can never rest easy until the
head is found. The circumstance I have told you occurred in the
second century, so, of course, the head must now be a skull, but
though we have, generation after generation, sought for it, it has
never been found. Well, some time ago an old crone passed this way
(she was for all the world like a banshee), and, pointing up at the
house, she said she would come again soon after the arrival of a
certain youth, of whom it has been predicted that he alone can find
Con Condon’s head. They say she has been seen about the place to-
day. Have you seen her, Major?”
“Look!” said Minor. “Isn’t there something dark sitting under the
great arbutus tree? Yes; surely there is. Look, Max.”
Max looked, and while he did so, the moon, breaking through a
cloud, lighted up the carriage drive in front of the house; while its
rays, falling upon an arbutus tree, distinctly revealed a dark figure
crouched beside it. It seemed to be a very old woman, sitting, Irish
fashion, with her chin resting on her knees, while she rocked herself
to and fro, and crooned out a wailing Irish keen. Her face (which
was very dark coloured) was turned towards the boys, and her large
features and long grey hair gave her a very uncanny appearance.
Seeing Dennis, she beckoned to him. “Come with me, Max,” said he;
“I’m awfully frightened.” “I’ll go, too,” said Harry. “And I.” “And I.” So
they all stepped through the open window, and were soon standing
round the old crone.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com