The ASTA team
Session -> Set Working Directory -> Choose Directory
(shortcut: Ctrl+Shift+H).Tools -> Global Options -> Choose Directory
.4.6 * (2 + 3)^4
## [1] 2875
a <- 4
a
## [1] 4
b <- c(2, 5, 7)
b
## [1] 2 5 7
s <- 1:4
s
## [1] 1 2 3 4
s <- seq(1, 4, by = 1)
a * b
## [1] 8 20 28
a + b
## [1] 6 9 11
b ^ 2
## [1] 4 25 49
sum(b)
## [1] 14
prod(b)
## [1] 70
The slides and all exercises in R (including the exam questions) are made in the special Rmarkdown format.
This allows you to combine text and R code.
You can write formulas using standard LaTeX commands.
Tools -> Install Packages
install.packages("mosaic")
library
command:library(mosaic)
mosaic
package which has a lot of convenient functions for this course (we will get back to that later). It also prints a lot of info about functions that have been changed by the mosaic
package, but you can safely ignore that.?<command>
:?sum
tab
to make Rstudio guess what you have started typing.help.search("plot")
To Source
button.Now we will have a look at a data set concerning the 1988 vote in Chile for or against Pinochet to continue as leader. The sample consists of 2700 voters randomly selected from the Chilenean population.
region
: The region in Chile where the voter livespopulation
: Population of the region.sex
: The gender of the voter.age
: The age of the voter.education
: Education level of the voter.income
: Monthly income of the voter.statusquo
: To which degree the voter supports the status quo.vote
: Should Pinochet continue? Y
= yes, N
= no, U
=undecided, A
= will abstain from voting.
More information about the data set may be found here.
Chile <- read.delim("https://asta.math.aau.dk/datasets?file=Chile.txt")
head(Chile)
## region population sex age education income statusquo vote
## 1 N 175000 M 65 P 35000 1.0 Y
## 2 N 175000 M 29 PS 7500 -1.3 N
## 3 N 175000 F 38 P 15000 1.2 Y
## 4 N 175000 F 49 P 35000 -1.0 N
## 5 N 175000 F 23 S 35000 -1.1 N
## 6 N 175000 F 28 P 7500 -1.0 N
head(Chile)
## region population sex age education income statusquo vote
## 1 N 175000 M 65 P 35000 1.0 Y
## 2 N 175000 M 29 PS 7500 -1.3 N
## 3 N 175000 F 38 P 15000 1.2 Y
## 4 N 175000 F 49 P 35000 -1.0 N
## 5 N 175000 F 23 S 35000 -1.1 N
## 6 N 175000 F 28 P 7500 -1.0 N
Quantitative variables in the Chile
data set:
population
, age
, income
, statusquo
Categorical variables:
region
, sex
, education
, vote
All the categorical variables are nominal except education
, which has three ordered categories (primary, secondary, post-secondary).
vote
we can use the function tally
from the mosaic
package (remember the package must be loaded via library(mosaic)
if you did not do so yet):tally( ~ vote, data = Chile)
## vote
## A N U Y <NA>
## 187 889 588 868 168
tally( ~ vote, data = Chile, format = "percent")
## vote
## A N U Y <NA>
## 6.9 32.9 21.8 32.1 6.2
formula
(characterized by the “tilde” sign ~
) to indicate that we want this variable from the dataset Chile
(without the tilde it would look for a global variable called vote
and use that rather than the one in the dataset).To get an overview over the relation between two categorical variables, we can make a cross tabulation.
To make a table of all combinations of the two factors vote
and sex
, we use tally
again:
tally( ~ vote + sex, data = Chile)
## sex
## vote F M
## A 104 83
## N 363 526
## U 362 226
## Y 480 388
## <NA> 70 98
tally( ~ vote | sex, data = Chile, format = "percent")
## sex
## vote F M
## A 7.5 6.3
## N 26.3 39.8
## U 26.3 17.1
## Y 34.8 29.4
## <NA> 5.1 7.4
gf_bar
from mosaic
. For each level of the factor, a box is drawn with the height proportional to the frequency (count) of the level.gf_bar( ~ vote, data = Chile)
gf_bar( ~ vote | sex, data = Chile)
In this data set, a car magazine tested the fuel consumption of 32 cars. The variable mpg
gives the fuel consumption in miles pr. gallon (the data set is from 1974).
The data set is built into R under the name mtcars
, so it does not need to be loaded before use.
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21 6 160 110 3.9 2.6 16 0 1 4 4
## Mazda RX4 Wag 21 6 160 110 3.9 2.9 17 0 1 4 4
## Datsun 710 23 4 108 93 3.9 2.3 19 1 1 4 1
## Hornet 4 Drive 21 6 258 110 3.1 3.2 19 1 0 3 1
## Hornet Sportabout 19 8 360 175 3.1 3.4 17 0 0 3 2
## Valiant 18 6 225 105 2.8 3.5 20 1 0 3 1
The way to get a first impression of a quantitative variable is to draw a histogram.
x
is made as follows:
x
to the maximum value of x
in an appropriate number of equal sized sub-intervals.Histogram of mpg
for the mtcars
data. The bins
option sets the number of subintervals to 10.
gf_histogram( ~ mpg, data = mtcars,bins=10)
Suppose a sample comes from a population having a continuous distribution with density function \(f\).
When the number of observations tends to infinity, we obtain a nice smooth curve, where the area below the curve is \(1\). This curve is exactly the probability density function \(f\).
mtcars
example. A summary of the fuel consumption mpg
can be retrieved using the favstats
function:favstats( ~ mpg, data = mtcars)
## min Q1 median Q3 max mean sd n missing
## 10 15 19 23 34 20 6 32 0
sd The sample standard deviation is \(6.03\). Recall that this was given by \[ s=\frac{1}{n-1}\sum_{i=1}^n (x_i-\bar{x})^2. \]
missing There are no missing values.
median The median (or 50-percentile) is the value such that half of the sample has lower values than the median and half the sample has larger values.
Q1 and Q3 will be introduced on later slides.
Both the mean and the median can be considered the center of a distribution. In a symmetric distribution (such as the normal distribution) they are equal, while in a skewed distribution, they tend to be different.
The mean, median and standard deviation are just some of the summaries that can be read of the favstats
output (shown on previous page). They may also be calculated separately in the following way:
Sample size of mpg
:
length(mtcars$mpg)
## [1] 32
mpg
:mean( ~ mpg, data = mtcars)
## [1] 20
mpg
:median( ~ mpg, data = mtcars)
## [1] 19
mpg
:sd( ~ mpg, data = mtcars)
## [1] 6
vs
) the sample mean is:mean( ~ mpg | factor(vs), data = mtcars)
## 0 1
## 17 25
If the histogram of the sample looks like a bell shaped curve, then we have
All or almost all (99.7%) of the observations lie between \(\bar{y}-3s\) and \(\bar{y}+3s\).
First, sort data from smallest to largest. For the mpg
variable: \[
x_{(1)}=10.4, x_{(2)}=10.4, x_{(3)}=13.3, \ldots, x_{(n)} = 33.9.
\] Here the number of observations is \(n=32\).
Recall
favstats( ~ mpg, data = mtcars)
## min Q1 median Q3 max mean sd n missing
## 10 15 19 23 34 20 6 32 0
How to draw a box-and-whiskers plot:
Outliers: Measurements smaller than L or larger than U are drawn as circles.
Note: Whiskers are minimum and maximum of the observations that are not deemed to be outliers.
favstats(mpg ~ vs, data = mtcars)
## vs min Q1 median Q3 max mean sd n missing
## 1 0 10 15 16 19 26 17 3.9 18 0
## 2 1 18 21 23 30 34 25 5.4 14 0
gf_boxplot(mpg ~ factor(vs), data = mtcars)
A scatter plot is used to visualize two quantitative variables.
For instance, we can plot the relation between fuel consumption and horse powers (hp
) of a car as follows
gf_point(mpg ~ hp, data = mtcars)
vs
:gf_point(mpg ~ hp | factor(vs), data = mtcars)
gf_point(mpg ~ hp, col = ~factor(vs), data = mtcars)
gf_point(mpg ~ hp, col = ~factor(vs), data = mtcars) %>% gf_lm()
The quantiles may be used to investigate whether the sample comes from a normal distribution.
Call the \(\frac{i}{n}\)th quantile of a standard normal distribution \(q_i\), i.e. \(P(Z\leq q_i)= \frac{i}{n}\).
If \(Y\sim \texttt{norm}(\mu,\sigma)\), then this is equivalent to is equivalent to \[P(Y\leq \mu+\sigma q_i)= \frac{i}{n}.\]
Suppose the population follows a \(\texttt{norm}(\mu,\sigma)\) distribution, then the sample quantiles \(x_{(i)}\) should be approximately equal to the population quantiles \(\mu+\sigma q_i\).
If we make a scatter plot of the pair \((q_i,x_{(i)})\), these should lie on a straight line. We call this a normal Q-Q plot (or quantile-quantile plot).
Example: We investigate whether the mpg
variable in the mtcars
data set follows a normal distribution:
qqnorm(mtcars$mpg)
qqline(mtcars$mpg)