William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M.
Page 1
R Tutorial
Contents
1. R Windows Environment 8. Stepwise Regression
2. Preparing Data for Analysis in R 9. Residual Analysis and Influence Diagnostics
3. Listing Data and Saving Output 10. Logistic Regression
4. Graphing Data 11. One-Way Analysis of Variance
5. Descriptive Statistics 12. Analysis of Variance for Factorial Design
6. Hypothesis Tests on Means 13. Time Series Forecasting
7. Simple Linear Regression
and Multiple Regression
1. R Windows Environment
The R programming language is a free open-source statistical software package.
Commands are typed in line by line, similar to a Texas Instruments calculator. Once
a line is entered, R will immediately compile (evaluate) it. To start, simply start R
and a console will open where commands are input. For the sake of simplicity, short
names will be used (‘‘x,’’ ‘‘y,’’ etc.) when labeling data objects, but these are just
labels and can always be adjusted.
The program can be downloaded for free at: [Link] The R
help feature will give further details about discussed commands:
> ? Command Name
Replace ‘‘Command Name’’ with the command of interest (e.g., ‘‘? mean’’). Addition-
ally, you can go to the CRAN website to find information on additional commands
not discussed. A useful starting point for further information is: [Link]
[Link]/doc/manuals/[Link].
2. Preparing Data for Analysis in R
In R data is stored in ‘‘objects’’ on which analyses are run. Two objects discussed here
are vectors and matrices. A vector stores the values for one variable. Matrices store
entire tables of data, consisting of multiple variables, and are useful for complicated
analyses. It is easier, especially with tables, to store data in a ‘‘.txt’’ file and import
it into R, instead of writing everything out.
To create a vector of one value, 5, in R and call it ‘‘x,’’ use the command
> x = 5
1
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 2
2 R Tutorial
This tells R to create an object called ‘‘x,’’ which equals the value 5. To create a
vector of the values {1,2,3,4} in R, use the command:
> x = c(1,2,3,4)
This creates a vector called ‘‘x’’ that contains the desired values. The command
‘‘c()’’ tells R to create a vector of the values in the parentheses (each value must
be separated by a comma). If desired, vectors can contain character values instead
of numeric by enclosing the desired characters in single quotes. You can see what
values are in any vector or matrix by typing out the name and pressing enter/return.
See Figure 1 for examples.
Figure 1 R commands for
creating vectors
A vector can be created by importing the data. To import data from a file and
save it as a vector, use the command:
> x = [Link]([Link](), header = FALSE)
A window will appear, as in Figure 2, where the file that contains the data can be
selected.
Figure 2 Importing data
from a file in R
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 3
Graphing Data 3
A matrix can be created by importing the data. Start by importing the data from
the file into R using the command:
> x = [Link]([Link](), header = TRUE)
If the data was laid out as a table in the data file, then ‘‘x’’ will also be a type of table
(called a data frame). Convert ‘‘x’’ into a matrix using the command:
> x = [Link](x)
3. Listing Data and Saving Output
The data stored in an object, ‘‘x,’’ can be displayed by typing ‘‘x’’ into the console
and pressing enter:
> x
To save a generated plot for use in a report, select the window containing
the plot (i.e., make sure the plot is selected and not the console) and go to
File → Save As.
Figure 3 Saving a plot in R
4. Graphing Data
To create a histogram of observations stored in the vector ‘‘x,’’ use the command:
> hist(x)
R will automatically decide how many bins to use for the histogram. To suggest how
many bins, use the command:
> hist(x, breaks=a)
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 4
4 R Tutorial
where ‘‘a’’ is the number of breaks to be used. The number of bins is equal to the
number of breaks plus one. R will take ‘‘a’’ as a suggestion and will sometimes
add more bins than asked for. For a histogram with a total area of one, add in
‘‘prob=TRUE’’:
> hist(x, breaks=a, prob=TRUE)
Figure 4 R commands for creating a histogram
To create a bar chart, first create a vector, call it ‘‘counts,’’ of the counts used
for each bar. Then use the command:
> barplot(counts)
This will create a very basic bar chart. A label can be added to each bar using:
> barplot(counts, [Link]=c(’bar1 name’,’bar2 name’))
where ‘‘bar1 name’’ and ‘‘bar2 name’’ should be replaced with the desired labels.
Make sure labels are in single quotes so that R knows that they are names not R
objects. If there are more than two bars, then include more labels by typing more
names after ‘‘bar2 name.’’
To create a pie chart, let the vector ‘‘counts’’ be a vector of the counts used for
each slice. Note that the counts can be replaced by proportions if desired. A basic
pie chart can be created using the command:
> pie(counts)
Add more informative labels to the pie chart using the command:
> pie(counts, labels = c(’name1’,’name2’))
The label ‘‘name1’’ should be replaced with the label desired for the first entry of
‘‘counts,’’ the second label ‘‘name2’’ should be replaced by the label desired for
the second entry of ‘‘counts.’’ If there are more than two entries in ‘‘counts,’’ then
continue to add labels after ‘‘name2.’’
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 5
Graphing Data 5
Figure 5 R commands for creating a bar graph
Figure 6 R commands for creating a pie chart
To construct a boxplot of ‘‘x’’ we use the command:
> boxplot(x)
To construct a scatterplot of two variables, call the first one ‘‘x’’ and the second
one ‘‘y,’’ use the command:
> plot(x,y)
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 6
6 R Tutorial
Figure 7 R commands for creating a box plot
Remember that whichever variable is listed first will be plotted on the horizontal
axis and the second variable will be on the vertical.
Figure 8 R commands for creating a scatterplot
5. Descriptive Statistics
Here are various R functions for summarizing quantitative variables. Let ‘‘x’’ be a
vector containing the observations.
Five Number Summary (also calculates the mean):
> summary(x)
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 7
Hypothesis Tests on Means 7
Quantiles (Percentiles):
> quantile(x,prob=a)
Replace ‘‘a’’ with the desired quantile (in decimal form).
Mean:
> mean(x)
Standard Deviation:
> sd(x)
Variance:
> var(x)
Let ‘‘y’’ be a second variable, the same length as ‘‘x.’’
Correlation:
> cor(x,y)
Covariance:
> cov(x,y)
Figure 9 R commands for
producing descriptive
statistics
6. Hypothesis Tests on Means
To perform a hypothesis test on means, use the command ‘‘t-test.’’ If ‘‘x’’ is the
vector of observations and ‘‘mu null’’ is the value of the mean under the null
hypothesis, then use the command:
>[Link](x, mu = mu null, alternative = ’[Link]’)$[Link]
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 8
8 R Tutorial
The above will return the p-value of the test. If a one-sided test is desired, then
replace ‘‘[Link]’’ with ‘‘greater’’ or ‘‘less.’’
Figure 10 R commands
for testing a mean
7. Simple Linear Regression
and Multiple Regression
To calculate a regression line where ‘‘y’’ is the dependent or response variable and
‘‘x’’ is the independent or explanatory variable, use the command:
> lm (y ∼ x) $coef
The above command will give the estimate for the intercept followed by the slope.
Figure 11 R commands
for simple linear regression
To perform a multiple regression where ‘‘y’’ is the dependent or response
variable and ‘‘x1’’ and ‘‘x2’’ are the independent or explanatory variables, use the
command:
> lm (y ∼ x1 + x2) $coef
The above will produce three numbers: the first will be the intercept, the second
will be the slope for ‘‘x1,’’ and the third will be the slope for ‘‘x2.’’ For more than
two variables continue to list them in the above command. Separate each variable
by a ‘‘+’’.
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 9
Stepwise Regression 9
Figure 12 R commands
for multiple regression
To calculate a multiple regression where ‘‘y’’ is the dependent or response
variable and ‘‘x matrix’’ is a matrix containing all of the explanatory variables
(each column of ‘‘x matrix’’ is a different variable) use the command:
> lm (y ∼ x matrix)$coef
The above command will produce multiple numbers: the first will be the intercept,
the second will be the slope for the first column of ‘‘x matrix,’’ the third will be
the slope for second column of ‘‘x matrix,’’ and so on.
8. Stepwise Regression
Let ‘‘y’’ be the dependent or response variable. Let ‘‘x1’’ and ‘‘x2’’ be two explanatory
or independent variables. To perform a backward stepwise regression, use the
command:
> step(lm(y ∼ x1 + x2),direction = ’backward’)
Figure 13 R commands
for stepwise regression
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 10
10 R Tutorial
Include additional variables after ‘‘x2,’’ separated with a ‘‘+’’. Note ‘‘backward’’
can be changed to ‘‘forward’’ or ‘‘both’’ for a forward or both stepwise regression.
9. Residual Analysis and Influence Diagnostics
If the index of the observations represents time (i.e., ‘‘x’’ and ‘‘y’’ are time ordered),
then a time-ordered residual plot can be created using the command:
> plot(lm(y ∼ x)$residuals)
If the index of ‘‘x’’ and ‘‘y’’ are not time ordered, then a more meaningful plot is:
> plot(x,lm(y ∼ x)$residuals)
This plots the residuals against the explanatory variable. If evaluating a multiple
regression, then replace ‘‘lm(y ∼ x) ’’ with the corresponding multiple regression
command.
Figure 14 R commands for plotting residuals
10. Logistic Regression
Let ‘‘y’’ be the binary independent or response variable and ‘‘x’’ be the explanatory
or dependent variable. To perform a logistic regression use the command:
> glm(y ∼ x,family = binomial)
11. One-Way Analysis of Variance
Let ‘‘x’’ be the vector of observations. Let ‘‘group’’ be a vector indicating which
group each ‘‘x’’ value belongs to. So the first entry of ‘‘group’’ indicates what group
the first value of ‘‘x’’ belongs to, and so on. R needs to be told that ‘‘group’’ is a
factor using the command:
> group = factor(group)
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 11
Analysis of Variance for Factorial Design 11
Figure 15 R commands
for logistic regression
To perform a one-way ANOVA, use the command:
> anova (lm(x ∼ group))
There are two commands above. The ‘‘lm’’ command, as seen before, tells R that
‘‘x’’ is the dependent or response variable and that ‘‘group’’ is the independent
factor. The ‘‘anova’’ command then tells R to carry out the one-way ANOVA on
‘‘x’’ given that ‘‘group’’ is the factor. R will display several pieces of information,
but the value listed under ‘‘P(>F)’’ is the p-value of the test.
Figure 16 R commands
for a one-way ANOVA
12. Analysis of Variance for Factorial Design
Let ‘‘x’’ be the vector of observations. If there are two factors, call them ‘‘factor1’’
and ‘‘factor2,’’ then a two-way ANOVA table without an interaction term can be
generated using the command:
> anova(lm(x ∼ factor1 + factor2))
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 12
12 R Tutorial
A two-way ANOVA table with an interaction term can be generated using the
command:
> anova(lm(x ∼ factor1 * factor2))
The term ‘‘factor1*factor2’’ tells R to include all main effects and interaction
terms in the model. R will display several pieces of information when performing an
ANOVA, but the values listed under ‘‘P(>F)’’ are the p-values. More factors can
be included after ‘‘factor2,’’ just separate each factor with a ‘‘ * ’’ to have R include
all main effects and all interaction terms.
Figure 17 R commands
for a factorial ANOVA
13. Time Series Forecasting
For moving average smoothing, first create a vector of weights to use in the
smoothing. Let ‘‘L’’ be the length of the moving average. For standard moving
average smoothing, each term in the average gets the same weight. The vector of
weights, call it ‘‘w,’’ can be constructed using the command:
> w = rep(1,times=L)/L
This command creates a vector of length ‘‘L’’ where each entry has value ‘‘1/L.’’
Different weights can be used by manually creating ‘‘w’’ with the desired weights.
To smooth ‘‘x’’ and store the values in ‘‘[Link]’’ use the command:
> [Link] = filter(x,filter=w,method= convolution )
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 13
Time Series Forecasting 13
Figure 18 R commands
for moving averages
For single exponential smoothing, first let ‘‘w’’ be the value of the exponential
smoothing constant. To smooth ‘‘x’’ and store the values in ‘‘[Link]’’ use the
command:
> [Link] = HoltWinters(x,gamma=0,beta=0,alpha=w)$fitted[,1]
The ‘‘[,1]’’ at the end is included to select the smoothed values.
Figure 19 R commands
for exponential smoothing
Let ‘‘x’’ be a vector of observations representing a time series. To fit an
autoregressive moving average model (ARMA) to ‘‘x’’ with order ‘‘p’’ for the AR
part and order ‘‘q’’ for the MA part, use the command:
> arima(x,order=c(p,0,q))
© 2012 Pearson Education Inc.
William Mendenhall [Link] V1 - 09/30/2010 4:16 P.M. Page 14
14 R Tutorial
The previous command will provide several pieces of information, but the coefficient
estimates are given in the first line under Coefficients.
Figure 20 R commands
for fitting ARMA models
© 2012 Pearson Education Inc.