Worksheet 3
MATH7016 The Nature of Data
Data Management
The Maternal Smoking and birth weight data is on vUWS in the file [Link]
birthwt = [Link]("[Link]")
We can look at the top few entries using the head function.
head(birthwt, n=10)
The data is stored in R in a [Link] it has two columns and 1226 rows. The functions dim, nrow and
ncol are useful in this regard.
dim(birthwt)
nrow(birthwt)
ncol(birthwt)
The variables in the [Link] have names as shown by head. We can also use the function names
names(birthwt)
Summaries
With the smoking status variable smoke, a sensible summary is to tabulate the counts
table(birthwt$smoke)
And we can barplot these;
barplot(table(birthwt$smoke))
For the weight data bwt, we can compute the mean, median, variance and standard deviation;
mean(birthwt$bwt)
median(birthwt$bwt)
var(birthwt$bwt)
sd(birthwt$bwt)
The range function actually gives the min and max, not there difference, but we can use diff to get this.
1
range(birthwt$bwt)
diff(range(birthwt$bwt))
Quartiles are examples of a more general concept of quantiles. The first and third quartiles are given by
quantile(birthwt$bwt, 1/4)
quantile(birthwt$bwt, 3/4)
The interquartile range can be computed from these or by using the function IQR
IQR(birthwt$bwt)
(For Normally distributed data - we cover this later - IQR = 1.349 × s)
For a quick look a many of these summary variables,
summary(birthwt$bwt)
Histograms
A simple way to plot a single quantitative variable is to use a histogram
hist(birthwt$bwt)
As with most of R plotting the are a plethora of options. To change the x-axis label to something prettier,
remove the annoying title, and colour in the bars we use;
hist(birthwt$bwt, xlab="Birth Weight", main="", col="lightblue")
To manually influence the number of bins/bars we use breaks
hist(birthwt$bwt, xlab="Birth Weight", main="", col="lightblue", breaks=20)
Splitting the data up
We obviously want to look at the data split by the smoking variable. The simplest way to do this is using a
function called aggregate. This takes something called a formula which describes the relatioship of interest,
the data and another function to use. For example,
aggregate(bwt~smoke, birthwt, mean)
This will split bwt up by smoke (two groups), out of the birthwt data, and apply the mean function to the
groups generated.
Even
2
aggregate(bwt~smoke, birthwt, summary)
Try using above with median and sd.
Split charts
To split a histogram by a grouping variable like smoke, the easiest way is to use an add-on package called
lattice. This is a very powerful visualisation package, but we need a small part of it. It is usually installed
with base R so
library(lattice)
histogram(~bwt|smoke, data=birthwt)
should work. Note the (slightly different) formula interface, and the fact that we have to use histogram not
hist.
Box plots
To make box plots, and comparative box plots the built in function boxplot is used.
boxplot(birthwt$bwt)
Sometimes horizontal plots are easier to see, and the open circles for outlying points can be changed. The
pch parameter determines the plotting character by number. Number 16 is a filled circle.
boxplot(birthwt$bwt, horizontal=TRUE, pch=16)
The boxplot function also has a formula interface, used when comparing groups.
boxplot(bwt ~smoke, data=birthwt, horizontal=TRUE, pch=16)
Difference in means
The actual difference in means can be computed from the aggregate data
diff(aggregate(bwt~smoke, birthwt, mean)$bwt)
Because the rows are alphabetically labelled, this is the yes-no difference. To get no-yes we make it negative.
-diff(aggregate(bwt~smoke, birthwt, mean)$bwt)
To simulate the smoking variable is very easy - we use sample again, this time without replacement (the
default) and generate a sample the same length as the original data. We then compute the difference in
means for this labelling of smoke.
3
[Link] = sample(birthwt$smoke)
-diff(aggregate(bwt~[Link], birthwt, mean)$bwt)
Use replicate to run 1000 or 10000 simulations.
Challenges
The data set [Link] contains the Sales data from lectures. Compute if there is a difference in the mean
sales between the East and West. Remember we are interested in whether average sales are just different.
The data set [Link] contains the anxiety levels of 24 arachnophobes when presented with either a
picture or a real spider. Does a picture induce less anxiety than the real thing?