Statistics with R
Summary Stats
• mean, min, max, range, quartile (fivenum),
IQR
• summary
• stem(x, scale=1,width = 80,atom = 1e-08)
– Scale controls length
– Width controls width
– Atom controls tolerance
Correlation
• cor(x, y = NULL, use = "everything",
method = c("pearson", "kendall",
"spearman"))
– use specifies how to treat NA values
– covariance command is similar (cov)
– Pearson is the standard method – works best
under normality; spearman is used particularly
with non-parametric data; kendall is rank
correlation
[Link]
Correlation
• Let us use Seatbelts data
– Explore the data – plot time series to see how it
has changed over time
– Correlation between driver deaths/serious injury
and
• distance travelled
• law
– New variable driver deaths/serious injury per km
driven; now compute correlations
– Correlation between distance travelled and petrol
price
t test
• One and two sample t tests
• [Link](x, y = NULL, alternative =
c("[Link]", "less", "greater"), mu = 0,
paired = FALSE, [Link] = FALSE,
[Link] = 0.95, ...)
– y = NULL is one sample test; compares if
mean of x equals mu
– specifying y gives comparison of means of
two samples
t test
• Seatbelts data again…
– Is the mean of front equal to 800?
– Are the means different before and after the
seat-belt law was introduced?
– Compare the results obtained with manual
computations
Comparing Variances
• Two samples from Normal population
– use [Link](….)
• Create a new variable winter and add to
Seatbelts as a [Link]; winter is 1 for
Jan, Feb, Dec months and 0 otherwise.
• Test if variances are different
– [Link](front ~ winter, data = dfSeatbelts)
[Link]
Comparing Variances
• bartlett’s test
– test if k samples are from populations with equal
variances (homoscedasticity)
• [Link]( front ~ winter, data = dfSeatbelts) gives what?
– create a variable seasons: Dec - Feb = Winter; Mar -
May = Spring; Jun – Sep = Summer, Oct – Nov = Fall
• what do you get in [Link] with seasons?
• can you use [Link]?
– Especially useful when doing ANOVA; ANOVA
requires homoscedasticity
Formula object and symbols
• y ~ x1 + x2 + x3
– y is a function of x1, x2 and x3
• y~x|z
– y is a function of x conditional on z
• ~ expresses relationship
• + indicates linear relationship
• +0 indicates intercept term is zero
• | indicates conditioning variable
• * indicates interactions between variables
• I() indicates interpret +, * as in an arithmetic
expression
ANOVA
• To compare means across more than two
groups
– aov( front ~ seasons, dfSeatbelts)
– summary( aov( front ~ seasons, dfSeatbelts) )
– summary( aov( front ~ seasons+law,
dfSeatbelts) )
– summary( aov( front ~ seasons*law,
dfSeatbelts) )
Test for Normality
• QQ Plot
• graphical technique
for determining if
two data sets come
from populations
with a common
distribution
[Link]
Shapiro Test
[Link]
K-S Test
[Link]
Test for normality
• To test if a data is distributed normally
– Is front coming from a normal distribution?
– you can also use qqplot and qqline to visually
see; hist also gives a visual idea of data
distribution
• Shapiro test
– [Link](x)
• Kolmogorov-Smirnov (KS) test
– [Link](dfSeatbelts$front, "pnorm",
mean(front), sd(front))
Chi square test
• [Link] = table(cut(front,8))
• [Link] = table(cut(rnorm(100),8))
• [Link]([Link], [Link])
• Try again
– Are the results the same?
– Comparing across the 3 tests?
lm
• lm(formula, data, subset, weights, [Link],
method = "qr", model = TRUE, x = FALSE, y =
FALSE, qr = TRUE, [Link] = TRUE,
contrasts = NULL, offset, ...)
• can be stored as an object
• summary([Link]) gives summary of
regression
• residuals([Link]) gives vector of residuals
• coef([Link]) gives co-efficient values
• [Link]([Link]) gives anova stats
Regression
• data(attitude)
• ?attitude
– Use the commands at the bottom
– pairs shows what? what do you expect from
the regression.
– run the two regression equations given there
– run complaints+learning
– run complaints+learning+advance