R Statistical Functions Overview
R Statistical Functions Overview
The most common tools used in statistics are means, variances, correlations, and t-tests. These
are used in R with easy-to-use functions such as mean, var., cor, and t-test.
18.1 Summary Statistics
Summary statistics are a set of measures that describe the main features of a dataset, giving a
quick overview of its distribution, central tendency, and spread.
Common summary statistics include:
• Minimum (Min)
• First Quartile (1st Qu.)
• Median (Median)
• Mean (Mean)
• Third Quartile (3rd Qu.)
• Maximum (Max)
• Variance(var)
• Correlation(cor)
• T-test
Using summary() in R
The summary() function in R provides all these statistics at once for numeric vectors, factors,
or entire data frames.
Example 1: Numeric Vector
x<-sample(x=1:100)
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 25.75 50.50 50.50 75.25 100.00
[1] 58 49 NA NA 17 55 96 32 45 62 88 NA 52 NA 78 85 100 NA 89 15 57 2 5
[24] 97 83 1 11 35 81 48 67 NA 14 NA NA 4 9 NA 91 66 99 7 92 75 NA NA
[47] 19 36 71 NA 29 6 72 21 50 90 68 33 24 25 NA NA 61 38 NA 87 94 69 77
[70] 8 12 18 98 NA 31 53 NA 37 82 84 NA 56 76 16 34 95 NA 3 74 80 13 46
[93] 93 51 22 40 73 20 39 NA
> mean(x)
[1] NA
1
> weights=c(1/2,1/4,1/8,1/8)
> mean(grades)
[1] 80
> [Link](x=grades,w=weights)
[1] 84.625
Formula for Weighted Mean
The weighted mean is a type of average where different values contribute differently
according to their weights
y=sample(x=1:100)
>y
[1] 1 5 52 14 93 30 55 33 53 46 91 26 37 92 27 69 61 78 39 10 97 9 82
[24] 4 58 75 41 28 38 12 25 62 19 3 73 65 17 51 45 79 40 29 31 89 20 71
[47] 2 96 66 21 24 86 36 74 54 63 85 83 8 100 44 56 47 77 34 59 90 42 80
[70] 84 16 7 70 22 99 94 57 18 87 95 6 13 68 72 98 60 88 23 81 76 11 43
[93] 48 49 32 64 67 50 15 35
> var(y)
[1] 841.6667
Variance Formula
>y
[1] 1 5 52 14 93 30 55 33 53 46 91 26 37 92 27 69 61 78 39 10 97 9 82
[24] 4 58 75 41 28 38 12 25 62 19 3 73 65 17 51 45 79 40 29 31 89 20 71
[47] 2 96 66 21 24 86 36 74 54 63 85 83 8 100 44 56 47 77 34 59 90 42 80
[70] 84 16 7 70 22 99 94 57 18 87 95 6 13 68 72 98 60 88 23 81 76 11 43
[93] 48 49 32 64 67 50 15 35
> sd(y)
[1] 29.01149
2
>x
[1] 58 49 NA NA 17 55 96 32 45 62 88 NA 52 NA 78 85 100 NA 89 15 57 2 5
[24] 97 83 1 11 35 81 48 67 NA 14 NA NA 4 9 NA 91 66 99 7 92 75 NA NA
[47] 19 36 71 NA 29 6 72 21 50 90 68 33 24 25 NA NA 61 38 NA 87 94 69 77
[70] 8 12 18 98 NA 31 53 NA 37 82 84 NA 56 76 16 34 95 NA 3 74 80 13 46
[93] 93 51 22 40 73 20 39 NA
> sd(x)
[1] NA
> sd(x,[Link]=TRUE)
[1] 30.88771
> min(y)
[1] 1
> max(y)
[1] 100
> median(y)
[1] 50.5
> min(x)
[1] NA
> min(x,[Link]=TRUE)
[1] 1
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
1.00 21.75 51.50 51.11 78.50 100.00 20
> summary(y)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 25.75 50.50 50.50 75.25 100.00
> quantile(y)
0% 25% 50% 75% 100%
1.0 25.75 50.50 75.25 100.00
> quantile(y,probs=c(.25,.75))
25% 75%
25.75 75.25
> quantile(x)
Error in [Link](x) :
missing values and NaN's not allowed if '[Link]' is FALSE
> quantile(x,[Link]=TRUE)
0% 25% 50% 75% 100%
3
1.00 21.75 51.50 78.50 100.00
> quantile(y,probs=c(.1,.25,.5,.75,.99))
10% 25% 50% 75% 99%
10.90 25.75 50.50 75.25 99.01
> quantile(x,probs=c(.1,.25,.5,.75,.99))
Error in [Link](x, probs = c(0.1, 0.25, 0.5, 0.75, 0.99)) :
missing values and NaN's not allowed if '[Link]' is FALSE
> quantile(x,probs=c(.1,.25,.5,.75,.99),[Link]=TRUE)
10% 25% 50% 75% 99%
8.90 21.75 51.50 78.50 99.21
Using cor() in R
Basic Syntax
cor(x, y, method = "pearson")
• x, y → numeric vectors
• method → correlation method:
o "pearson" → default, measures linear correlation
o "spearman" → rank-based correlation (non-parametric)
o "kendall" → rank correlation, less sensitive to ties
[Link]("ggplot2")
library(ggplot2)
head(economics)
date pce pop psavert uempmed unemploy
1 1967-07-01 506.7 198712 12.6 4.5 2944
2 1967-08-01 509.8 198911 12.6 4.7 2945
3 1967-09-01 515.6 199113 11.9 4.6 2958
4 1967-10-01 512.2 199311 12.9 4.9 3143
5 1967-11-01 517.4 199498 12.8 4.7 3066
6 1967-12-01 525.1 199657 11.8 4.8 3018
4
[1] -0.7928546
To compute Pearson’s coefficient, we multiply deviations from the mean for X times those
for Y and divide by the product of the standard deviations. Here is the formula:
> xpart<-economics$pce-mean(economics$pce)
> ypart<-economics$psavert-mean(economics$psavert)
> n<-(nrow(economics)-1)
> xsd<-sd(economics$pce)
> ysd<-sd(economics$psavert)
> sum(xpart*ypart)/(n*xsd*ysd)
[1] -0.7928546
> cor(economics[,c(2,4:6)])
pce psavert uempmed unemploy
pce 1.0000000 -0.7928546 0.7269616 0.6145176
psavert -0.7928546 1.0000000 -0.3251377 -0.3093769
uempmed 0.7269616 -0.3251377 1.0000000 0.8693097
unemploy 0.6145176 -0.3093769 0.8693097 1.0000000
To visualize the information using a plot. ggpairs from the GGally package is used to
visualize this.
[Link]("GGally")
library(GGally)
ggpairs(economics[, c(2,4:6)])
5
Figure 18.1 Pairs plot of economics data showing the relationship between each pair of
variables as a scatterplot with the correlations printed as numbers.
This is similar to a small multiples plot except that each pane has different x and y axes. This
plot displays the original data, but it does not actually show the correlation. To show this, we
build a heatmap of the correlation numbers as shown in fig 18.2.
• High positive correlation shows a positive relationship between variables.
• High negative correlation indicates a relationship between the variables, and
• Non-zero correlation shows no strong relationship.
> library(reshape2)
> library(scales)
> econcor=cor(economics[,c(2,4:6)])
> econmelt=melt(econcor,varnames=c("x","y"),[Link]="Correlation")
> print(econmelt)
x y Correlation
1 pce pce 1.0000000
2 psavert pce -0.7928546
3 uempmed pce 0.7269616
4 unemploy pce 0.6145176
5 pce psavert -0.7928546
6 psavert psavert 1.0000000
7 uempmed psavert -0.3251377
8 unemploy psavert -0.3093769
9 pce uempmed 0.7269616
10 psavert uempmed -0.3251377
11 uempmed uempmed 1.0000000
12 unemploy uempmed 0.8693097
13 pce unemploy 0.6145176
14 psavert unemploy -0.3093769
15 uempmed unemploy 0.8693097
16 unemploy unemploy 1.0000000
> econmelt=econmelt[order(econmelt$Correlation),]
> print(econmelt)
x y Correlation
2 psavert pce -0.7928546
6
5 pce psavert -0.7928546
7 uempmed psavert -0.3251377
10 psavert uempmed -0.3251377
8 unemploy psavert -0.3093769
14 psavert unemploy -0.3093769
4 unemploy pce 0.6145176
13 pce unemploy 0.6145176
3 uempmed pce 0.7269616
9 pce uempmed 0.7269616
12 unemploy uempmed 0.8693097
15 uempmed unemploy 0.8693097
1 pce pce 1.0000000
6 psavert psavert 1.0000000
11 uempmed uempmed 1.0000000
16 unemploy unemploy 1.0000000
1. Loading libraries
library(reshape2) # for melting the data
library(scales) # for muted() colors and scaling functions
• reshape2: provides the melt() function that reshapes a wide matrix/data frame into a
long format (needed for ggplot2 heatmaps).
• scales: provides helpers like muted("red"), color scales, and formatting functions.
4. Ordering by correlation
econmelt = econmelt[order(econmelt$Correlation), ]
print(econmelt)
• order() sorts rows by correlation values.
• This is helpful if you want to inspect strongest/weakest correlations in tabular form.
• For plotting, order doesn’t affect the heatmap (but it helps when printing the table).
7
mid = "white", # neutral
high = "steelblue", # positive correlations
guide = guide_colorbar(ticks = FALSE, barheight = 10),
limits = c(-1, 1) # fix the scale between -1 and 1
)+
theme_minimal() +
labs(x = NULL, y = NULL)
• geom_tile() draws squares for each (x,y) pair.
• scale_fill_gradient2() maps correlations to colors:
o red = strong negative correlation
o white = no correlation
o blue = strong positive correlation
• theme_minimal() gives a clean theme.
• labs(x=NULL, y=NULL) removes axis labels (since variable names are already
visible).
Figure 18.2 Heatmap of the correlation of the economics data. Diagonal -with correlation 1.
Red indicates a highly negative correlation, blue indicates a highly positive correlation, and
white indicates no correlation.
8
Options for use
1. "everything" (default)
• Assumes that the data has no missing values.
• If any NA is present, the result will be NA.
• Strictest option.
cor(df, use = "everything")
2. "[Link]"
• Assumes there are no missing values in the dataset.
• If missing values are found, R will throw an error.
• Safe only when data is already cleaned.
cor(df, use = "[Link]")
3. "[Link]"
• Uses only the rows that have no missing values in any variable.
• Produces a full correlation matrix without NAs.
• May reduce the dataset size, but ensures consistency.
cor(df, use = "[Link]")
4. "[Link]"
• Similar to "[Link]", but avoids unnecessary checks if there are no missing
values at all.
• Slightly faster than "[Link]".
cor(df, use = "[Link]")
5. "[Link]"
• Uses all available pairs of variables.
• If two variables have missing data in different places, R still computes the correlation
for the available pairs.
• Maximizes data usage but may result in a correlation matrix that is not mathematically
consistent (not positive definite).
cor(df, use = "[Link]")
Summary Table
Example
Step 1: Create the vectors
m <- c(9, 9, NA, 3, NA, 5, 8, 1, 10, 4)
9
n <- c(2, NA, 1, 6, 6, 4, 1, 1, 6, 7)
p <- c(8, 4, 3, 9, 10, NA, 3, NA, 9, 9)
q <- c(10, 10, 7, 8, 4, 2, 8, 5, 5, 2)
r <- c(1, 9, 7, 6, 5, 6, 2, 7, 9, 10)
• Each vector has 10 elements.
• Some of them contain NA values (m, n, p).
2. "[Link]"
cor(mat, use = "[Link]")
• Requires no missing values at all.
• Since m, n, and p contain NA, R throws an error:
Error in cor(mat, use = "[Link]") : missing observations in cov/cor
3. "[Link]"
cor(mat, use = "[Link]")
• Uses only rows with no missing values across all variables.
• Here, the complete rows are: 1, 4, 7, 9, 10 (5 rows).
• Correlation is computed on these.
Result:
m n p q r
m 1.0000000 -0.5228840 -0.2893527 0.2974398 -0.3459470
10
n -0.5228840 1.0000000 0.8090195 -0.7448453 0.9350718
p -0.2893527 0.8090195 1.0000000 -0.3613720 0.6221470
q 0.2974398 -0.7448453 -0.3613720 1.0000000 -0.9059384
r -0.3459470 0.9350718 0.6221470 -0.9059384 1.0000000
4. "[Link]"
cor(mat, use = "[Link]")
• Same as "[Link]".
• Gives identical result (just skips some checks internally).
Result:
m n p q r
m 1.0000000 -0.5228840 -0.2893527 0.2974398 -0.3459470
n -0.5228840 1.0000000 0.8090195 -0.7448453 0.9350718
p -0.2893527 0.8090195 1.0000000 -0.3613720 0.6221470
q 0.2974398 -0.7448453 -0.3613720 1.0000000 -0.9059384
r -0.3459470 0.9350718 0.6221470 -0.9059384 1.0000000
Automatically selects only the rows that have no NA values across all columns.
cor(mat[c(1,4,7,9,10), ])
Here, you manually select the same rows that are complete.
identical() - Checks if the two correlation matrices are exactly the same (same values, same
dimensions).
Returns TRUE → confirms that [Link] is equivalent to manually using only complete
rows.
6. [Link]
11
• Instead of removing entire rows with any NA (like "[Link]"), each pair of
variables uses all rows where both values are available.
• This allows maximum usage of the available data and avoids discarding rows
unnecessarily.
• As a result, the correlation matrix can be more "complete" even if individual rows have
NAs.
cor(mat, use="[Link]")
Output:
m n p q r
m 1.00000000 -0.02511812 -0.3965859 0.4622943 -0.2001722
n -0.02511812 1.00000000 0.8717389 -0.5070416 0.5332259
p -0.39658588 0.87173889 1.0000000 -0.5197292 0.1312506
q 0.46229434 -0.50704163 -0.5197292 1.0000000 -0.4242958
r -0.20017222 0.53322585 0.1312506 -0.4242958 1.0000000
Code Example
cor(mat[, c("m", "n")], use = "[Link]")
Explanation
1. Selecting Columns
o mat[, c("m", "n")] selects only the m and n columns from the matrix, producing
a smaller matrix with 2 columns and 10 rows.
2. Handling Missing Values with [Link]
o "[Link]" keeps only rows where both m and n have non-missing
values.
o In this example, rows 1, 4, 6, 7, 8, 9, 10 are complete. Rows with at least one
NA (2, 3, 5) are removed.
3. Correlation Calculation
o Correlation is computed using only the complete rows:
m n
m 1.00000000 -0.02511812
n -0.02511812 1.00000000
o cor(m, m) and cor(n, n) = 1
o cor(m, n) = -0.0251 → very weak negative correlation based on complete data.
12
m = 9, 9, NA, 3, NA, 5, 8, 1, 10, 4
p = 8, 4, 3, 9, 10, NA, 3, NA, 9, 9
• Complete rows (both values non-NA) are: 1, 2, 4, 7, 9, 10
• Rows 3, 5, 6, 8 contain NA in at least one column → removed.
GGally::ggpairs(tips)
• GGally::ggpairs() creates a pairwise plot matrix:
o Each variable is plotted against every other variable.
o Diagonal: usually histograms or density plots of each variable.
o Lower triangle: scatterplots showing relationships between numeric variables.
o Upper triangle: often shows correlation coefficients or smoothed plots.
13
o Categorical variables are handled automatically (e.g., colored points,
boxplots).
• Helps visually explore correlations and distributions for all variables in one plot.
Example output:
$img
[1] "[Link]
$title
14
[1] "Pressure"
$alt
[1] "Not to be confused with 'force per unit area.'"
$num
[1] 552
$day
[1] 5
$month
[1] 9
$year
[1] 2009
Note:
To get the details, visit the page
• Go to [Link]
• Each comic URL contains the comic number:
Example: [Link]
Example in R:
x <- c(12, 14, 15, 13, 16)
[Link](x, mu=14) # test if mean is 14
2. Two-Sample T-Test
o Compares the means of two independent groups.
o Formula:
15
o
Example in R:
group1 <- c(12, 14, 15)
group2 <- c(10, 11, 13)
[Link](group1, group2) # independent samples
3. Paired T-Test
o Compares means of two related groups (e.g., before and after treatment).
o Formula:
Example in R:
before <- c(85, 90, 88)
after <- c(88, 92, 89)
[Link](before, after, paired=TRUE)
16
Assumptions of T-Tests
1. Data are continuous (interval/ratio).
2. Data are approximately normally distributed.
3. Observations are independent.
4. For two-sample t-tests, population variances are equal (can be relaxed with [Link]
= FALSE in R).
Advantages
• Simple and widely used.
• Can handle small sample sizes.
• Helps compare sample mean with population or another sample.
Disadvantages
• Sensitive to non-normal data in small samples.
• Assumes independent observations (except for paired t-test).
• Not suitable for categorical data.
Examples:
[Link](tips$tip, alternative = "[Link]", mu = 2.50)
This is performing a one-sample t-test. Here's what each part means:
[Link]()
This is the built-in R function for performing a t-test, which is used to compare means.
Depending on the input, it can do:
• One-sample t-test
• Two-sample t-test
• Paired t-test
tips$tip
This is the data vector we are testing. In this case:
tips is a dataset (probably the famous “tips” dataset from restaurants).
tips$tip refers to the column containing the tip amounts.
alternative = "[Link]"
This specifies the alternative hypothesis:
"[Link]" → Tests whether the mean of the sample is not equal to the hypothesized mean.
"less" → Tests if the mean is less than the hypothesized mean.
"greater" → Tests if the mean is greater than the hypothesized mean.
mu = 2.50
17
head(tips)
total_bill tip sex smoker day time size
1 16.99 1.01 Female No Sun Dinner 2
2 10.34 1.66 Male No Sun Dinner 3
3 21.01 3.50 Male No Sun Dinner 3
4 23.68 3.31 Male No Sun Dinner 2
5 24.59 3.61 Female No Sun Dinner 4
6 25.29 4.71 Male No Sun Dinner 4
> unique(tips$sex)
[1] Female Male
Levels: Female Male
> unique(tips$day)
[1] Sun Sat Thur Fri
Levels: Fri Sat Sun Thur
> [Link](tips$tip,alternative="[Link]",mu=2.50)
data: tips$tip
t = 5.6253, df = 243, p-value = 5.08e-08
alternative hypothesis: true mean is not equal to 2.5
95 percent confidence interval:
2.823799 3.172758
sample estimates:
mean of x
2.998279
randT=rt(30000,df=NROW(tips)-1)
tipttest=[Link](tips$tip,alternative="[Link]",mu=2.50)
18
ggplot([Link](x=randT))+geom_density(aes(x=x),fill="grey",color="grey")+geom_vline(
xintercept=tipttest$statistic)+geom_vline(xintercept=mean(randT)+c(-
2,2)*sd(randT),linetype=2)
t-distribution and t-statistic for tip data. The dashed lines are two standard deviations from
mean in either directions. The thick black line, the t-statistic, is so far outside the distribution
that we must reject the null hypothesis and conclude that the true mean is not $2.50.
Explanation
1. Generate a random t-distribution
randT = rt(30000, df = NROW(tips) - 1)
• rt() generates random numbers from a t-distribution.
• 30000 → we are simulating 30,000 random t-values (a large sample to approximate
the distribution).
• df = NROW(tips)-1 → degrees of freedom is the number of rows in tips dataset minus
1.
o If tips has 244 rows, then df = 243.
This gives us a "null distribution" of t-values under the assumption that the null hypothesis is
true.
19
ggplot([Link](x=randT)) +
geom_density(aes(x=x), fill="grey", color="grey") +
geom_vline(xintercept=tipttest$statistic) +
geom_vline(xintercept=mean(randT)+c(-2,2)*sd(randT), linetype=2)
Explanation of layers:
• geom_density(...)
→ Plots the simulated t-distribution (the null distribution of t-values).
• geom_vline(xintercept = tipttest$statistic)
→ Adds a vertical line at the observed t-value from the real data.
This shows where your observed test statistic lies relative to the null distribution.
• geom_vline(xintercept = mean(randT) + c(-2,2)*sd(randT), linetype=2)
→ Adds two dashed vertical lines at:
mean of null distribution±2×standard deviation\text{mean of null distribution} \pm 2 \times
\text{standard deviation}mean of null distribution±2×standard deviation
This roughly corresponds to the ±2 standard deviation region (like a 95% interval under
normal approximation).
To conclude:
This code simulates a t-distribution under the null hypothesis, runs a one-sample t-test on
tips, and plots:
• The null distribution of t-values.
• The observed t-statistic from your data.
• Approximate 95% cutoff lines.
That way, you can visually check whether your observed t-value is far into the tails (→
small p-value) or close to the null distribution center.
data: tips$tip
t = 5.6253, df = 243, p-value = 2.54e-08
alternative hypothesis: true mean is greater than 2.5
95 percent confidence interval:
2.852023 Inf
sample estimates:
mean of x
2.998279
20
Here’s a visual explanation of your one-sample t-test:
• The blue dashed line represents your calculated t-value (5.6253).
• The green dashed line is the critical t-value for a 95% confidence, one-tailed test.
• The red shaded area is the rejection region where you would reject the null hypothesis.
Since the blue line (t-value) is far to the right of the critical value, it clearly falls in the rejection
region, confirming that the mean tip is significantly greater than 2.50.
The Shapiro-Wilk test is a statistical test used to check whether a dataset is normally
distributed. In other words, it tests if your data roughly follows a bell-shaped curve.
Here’s a detailed breakdown:
1. Purpose
• Many statistical tests (like t-tests, ANOVA) assume normality of the data.
• The Shapiro-Wilk test helps you check if this assumption is valid.
2. Hypotheses
Hypothesis Meaning
H₀ (null) The data is normally distributed
H₁ (alternative) The data is not normally distributed
3. Test Statistic
• Denoted as W.
• Ranges between 0 and 1.
o W close to 1: Data is close to normal.
o W far from 1: Data deviates from normality.
21
4. p-value
• If p-value > 0.05 → Fail to reject H₀ → Data is approximately normal.
• If p-value ≤ 0.05 → Reject H₀ → Data is not normal.
5. R Example
[Link](tips$tip)
• Suppose it returns W = 0.89781, p-value = 8.2e-12
• Interpretation:
o W = 0.89781 → Some deviation from normality
o p-value = 8.2e-12 → Extremely small → Reject H₀
o Conclusion: Tip data is not normally distributed
aggregate(tip~sex,data=tips,var)
sex tip
1 Female 1.344428
2 Male 2.217424
> [Link](tips$tip[tips$sex=="Female"])
> [Link](tips$tip[tips$sex=="Male"])
Meaning:
• Null hypothesis (H₀): Male customers’ tips are normally distributed.
• Alternative hypothesis (H₁): Male customers’ tips are not normally distributed.
• Test statistic (W) = 0.87587 → Far from 1, indicates departure from normality.
• p-value = 3.708e-10 (< 0.05) → Reject H₀.
Conclusion: The distribution of tips given by male customers is not normal.
22
> ggplot(tips,aes(x=tip,fill=sex))+geom_histogram(binwidth=.5,alpha=1/2)
Since the data do not appear to be normally distributed, standard F-test nor bartlett test will
suffice. Hence Ansari-Bradley test is used to examine the equality of variances.
[Link](tip~sex,tips)
Ansari-Bradley test
This test indicates that the variances are equal, that is we can use the standard two sample t-
test.
[Link](tip~sex,data=tips,[Link]=TRUE)
23
Two Sample t-test
Based on the test, the results are nor significant and we conclude that male and female
dinners tip roughly equal.
To check if the two means are within the two standard deviations of each other.
[Link]("plyr")
library(plyr)
tipSummary=ddply(tips,"sex",summarize,[Link]=mean(tip),[Link]=sd(tip),Lower=[Link]
-2*[Link]/sqrt(NROW(tip)),Upper=[Link]+2*[Link]/sqrt(NROW(tip)))
tipSummary
This line is doing group-wise summary statistics with ddply() from the plyr package.
Step-by-Step Explanation
1. ddply(tips, "sex", summarize, ...)
• ddply() splits the data frame tips by the factor sex ("Male" and "Female").
• Then applies the functions inside summarize to each group.
• Finally returns a new data frame with results for each group.
2. [Link] = mean(tip)
• Calculates the average tip separately for males and females.
3. [Link] = sd(tip)
• Computes the standard deviation of tips for each sex.
24
• The confidence intervals show the range where the true mean tip is likely to fall for each group.
• If the intervals overlap → difference may not be statistically significant.
• If they don’t overlap → stronger evidence of a real difference.
>ggplot(tipSummary,aes(x=[Link],y=sex))+geom_point()+geom_errorbarh(aes(xmin=Low
er,xmax=Upper),height=.2)
• A Paired t-test compares the means of two related groups (not independent).
• Instead of comparing the groups directly, it looks at the differences within each pair.
We use a Paired t-test when the two samples are dependent / matched:
Examples:
• Before vs After measurements on the same subjects (e.g., blood pressure before and
after treatment).
• Left vs Right measurements from the same person (e.g., left eye vs right eye).
• Father vs Son heights (each father is naturally paired with his son).
• Two methods tested on the same participants (e.g., exam scores of students under two
teaching methods).
In all these cases, observations are linked one-to-one.
[Link]("UsingR")
library("UsingR")
data([Link],package="UsingR")
head([Link])
fheight sheight
1 65.04851 59.77827
2 63.25094 63.21404
3 64.95532 63.34242
4 65.75250 62.79238
5 61.13723 64.28113
6 63.02254 64.24221
25
[Link]([Link]$fheight,[Link]$sheight,paired=TRUE)
• Performs a paired t-test between:
o [Link]$fheight → fathers’ heights
o [Link]$sheight → sons’ heights
• paired=TRUE means we are comparing matched pairs (each father with his own
son).
Paired t-test
2. t-value
• t = -11.789
• This is very large in magnitude → the difference is highly significant.
• It means the observed difference is almost 12 standard errors away from 0.
3. Degrees of Freedom
• df = 1077 → 1,078 pairs (father-son).
• df=n−1df = n-1df=n−1, where nnn = number of pairs.
4. p-value
• p-value < 2.2e-16 (essentially 0).
• Much smaller than 0.05 → Reject H₀.
• Strong evidence of a real difference in heights between fathers and sons.
5. Confidence Interval
• 95% CI: [−1.163,−0.831][-1.163, -0.831][−1.163,−0.831]
26
• Since the interval does not include 0, the difference is statistically significant.
• Interpretation: With 95% confidence, sons are between 0.83 and 1.16 inches taller
than fathers.
The paired t-test shows that sons are significantly taller than their fathers, by about 1 inch
on average.
heightdiff=[Link]$fheight- [Link]$sheight
ggplot([Link],aes(x=fheight-sheight)) +
geom_density() +
geom_vline(xintercept=mean(heightdiff)) +
geom_vline(xintercept=mean(heightdiff) + 2*c(-1,1)*sd(heightdiff)/sqrt(nrow([Link])),
linetype=2)
Interpretation
heightdiff=[Link]$fheight- [Link]$sheight
• This takes the difference between father’s height (fheight) and son’s height
(sheight) for each pair.
• So heightdiff = vector of differences.
• Positive → father taller than son.
• Negative → son taller than father.
ggplot creates a plot using [Link] dataset.
• aes(x = fheight - sheight) → the x-axis is the difference in heights.
• geom_density() → draws a smooth density curve (like a smoothed
histogram) showing the distribution of these differences.
geom_vline(xintercept = mean(heightdiff))
• mean(heightdiff) → average difference in height.
• A solid vertical line is drawn at this value.
• Shows whether, on average, fathers are taller or shorter than sons.
geom_vline( xintercept = mean(heightdiff) + 2 * c(-1, 1) * sd(heightdiff) /
sqrt(nrow([Link])), linetype = 2 )
• sd(heightdiff) → standard deviation of differences.
• sqrt(nrow([Link])) → square root of sample size (for standard error).
• 2 * ... → multiplying by 2 gives approximately a 95% confidence interval for
the mean (by normal approximation).
• c(-1,1) → gives both sides (mean - margin, mean + margin).
• linetype = 2 → dashed vertical lines.
So these two dashed lines mark the 95% confidence interval for the mean difference in
height.
27
Interpretation of the Plot
• The density curve shows the distribution of (father’s height − son’s height).
• The solid line marks the average difference.
• The two dashed lines show the 95% confidence interval of the mean difference.
• If the CI does not include 0, it suggests fathers and sons have a statistically significant
difference in average height.
18.4 ANOVA
ANOVA (Analysis of Variance) is a statistical test used to compare the means of
three or more groups to see if at least one group mean is significantly different.
It is an extension of the t-test:
• t-test → compares 2 groups.
• ANOVA → compares 3 or more groups.
Hypotheses in ANOVA
• H₀ (null): All group means are equal.
• H₁ (alternative): At least one group mean is different.
Types of ANOVA
1. One-way ANOVA → One independent variable (factor) with ≥ 3 groups.
2. Two-way ANOVA → Two independent variables (factors), may include interaction
effects.
3. Repeated Measures ANOVA → Same subjects measured under different conditions.
ANOVA in R
1. One-way ANOVA Example
Suppose we check if tip amount differs by day in the tips dataset:
model <- aov(tip ~ day-1,tips)
summary(model)
28
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
tipintercept=aov(tip~day,tips)
model$coefficients
Here:
• tip ~ day means you are modeling tip as a function of the categorical variable day.
• In R, categorical variables (factors) are converted into dummy variables in the model
matrix.
tipintercept$coefficients
(Intercept) daySat daySun dayThur
2.73473684 0.25836661 0.52039474 0.03671477
3. Similarly:
o daySun = 0.52039474 → difference Sun vs Fri
o dayThur = 0.03671477 → difference Thur vs Fri
So aov() stores the regression coefficients relative to the baseline, not the raw means.
tipsbyday=ddply(tips,"day",plyr::summarize,[Link]=mean(tip),[Link]=sd(tip),Length=NR
OW(tip),tfrac=qt(p=.90,df=Length-1),Lower=[Link] -
tfrac*[Link]/sqrt(Length),Upper=[Link]+tfrac*[Link]/sqrt(Length))
Step-by-step explanation
1. ddply(tips, "day", ...)
o ddply splits the tips data frame by the variable day.
o For each day (Fri, Sat, Sun, Thur), it will compute the summaries you define.
2. plyr::summarize
o This tells ddply to create a summary data frame with new columns.
3. [Link] = mean(tip)
o Computes the average tip for that day.
4. [Link] = sd(tip)
o Computes the standard deviation of tips for that day.
29
5. Length = NROW(tip)
o Length stores the number of observations (tips) for that day.
6. tfrac = qt(p = .90, df = Length - 1)
o qt() gives the t-value for the 90th percentile (one-sided) of the t-distribution
with df = Length - 1 degrees of freedom.
o This is used to calculate a 90% confidence interval.
7. Lower = [Link] - tfrac * [Link] / sqrt(Length)
o The lower bound of the 90% confidence interval.
8. Upper = [Link] + tfrac * [Link] / sqrt(Length)
o The upper bound of the 90% confidence interval.
summary(tipsbyday)
ggplot(tipsbyday,aes(x=[Link],y=day))+geom_point() +
geom_errorbarh(aes(xmin=Lower,xmax=Upper),height=.3)
30
1. nrow(tips)
• tips is a data frame (like a table).
• nrow() returns the number of rows in a data frame or matrix.
• Output:
[1] 244
The tips dataset has 244 rows.
2. nrow(tips$tip)
• tips$tip extracts just the tip column, which is a vector of length 244.
• nrow() works only on 2D objects (matrices, data frames).
• Since a vector has no rows/columns, nrow() returns:
NULL
3. NROW(tips$tip)
• NROW() is more general than nrow().
• It works on vectors, matrices, or data frames.
• For a vector, NROW(x) = length(x).
• Output:
[1] 244
Because the tip vector has 244 elements.
Key Difference
• nrow() → Only for 2D objects (data frames, matrices). Returns NULL for vectors.
• NROW() → More flexible. Works for vectors too, returns their length.
Conclusion
• ANOVA is a powerful tool for comparing means of multiple groups.
• It generalizes the t-test and uses the F-statistic.
31