INTRODUCTION TO R Commands
The best way to learn R is to use it. In today's lab you'll use R using a list of commands given below. By examining the output, you can figure out for yourself what the commands are doing and thus learn something about R.
#Transformations and Graphical Diagnostics
#Also introduces the loess function
#Data: brain (g) and body (kg) weights for 62 species of mammals
Data from Weisbery, Applied Linear Regression, edition 1, page 144
These data are available on the class
# Code for Chapter 1 - Problem 19
#Read in the data (remember: use right slash if in Windows)
ch1pr19.dat <- read.table("YOUR PATH/CH01PR19.txt")
#NOTE: It is beter to use real titles like "ACT" and "GPA" for the
#variable names instead of the gene
#_
# Problem 1.23
# a. Obtain the residuals and check the sum.
sum(lm.ch1pr19$res)
#_
# b. Point estimates of sigma^2 and sigma
# three ways:
mse = sum(lm.ch1pr19$res^2)/(length(x)-2)
mse #point estimate of sigma^2
sqrt(mse) #point estimate o
# Chapter 2 - Confidence Interval Problems
#BACKGROUND
# In order to compute confidence intervals, we can either do the
# computations manually (using using the qt() function), or we can use
# the confint function.
# The qt() function is the q
# Chapter 2, Problem 27
# Hypothesis Test for problem 2.27 part a
# Muscle Mass data
# X = age
# Y = measure of muscle mass
#Read in and examine the data
ch1pr27.dat <- read.table("Your path/KNN_data/chap1/CH01PR27.txt")
names(ch1pr27.dat) <- c("m
# Problem 2.28
# Note: see problem 2.27 where I read in the data
# Part a. Obtain a 95% confidence interval for the mean muscle mass
# for women of age 60
#Manual method:
std.err.y60 <- 8.173*sqrt(1/60 + (60-mean(age)^2/sum(age-mean(age)^2)
s
# Chapter 2, Problem 29
# The data for this problem are in from Chp 1, problem 27
names(ch1pr27.dat) <- c("mass","age")
attach(ch1pr27.dat)
# The fitted regression line is fit.1.27
fit.1.27 <- lm(mass~age)
# Part a.
par(mfrow=c(2,2)
plot(age,ma
#Illustrating various residual plots
# Model Y_i = 2 + 5*X_i + epsilon_i
# Where N(0,4^2)
# Fix X's from 11 to 18
set.seed(1) #This gives me the same randomly generated data every time
x = runif(25,11,18)
err.a = rnorm(25,0,4)
y = 2 + 5*x + err.
ST 540: An Introduction to R
Ryan T. Elmore and Jennifer A. Hoeting August 22, 2007
General R Info
We will use the R statistical software in STAT 540. R is a platform-independent (runs on Windows, Mac, and Unix/Linux), freeware version of S-Plus. Yo