R code

The R code appearing in slides will be provided in reverse chronological order (like the posts). R code for quizzes will be in the page “quizzes”.

Day 4

summary(mtcars$mpg)
quartiles=quantile(mtcars$mpg); quartiles
iqr=quartiles[4]-quartiles[2]; iqr #using definition
IQR(mtcars$mpg) #using builtin function
ave = mean(mtcars$mpg)
deviations = mtcars$mpg - ave; deviations
sum(deviations)
SRR = c(0.9, 1.4, 1.2, 1.2, 1.3, 2.0, 1.4, 1.6); ave=mean(SRR)
deviations = SRR - mean(SRR); deviations
plot(deviations,col="blue",ylab="cycles per second",main="deviations from mean for Snake data")
xdata=c(1:length(SRR))
aveVec=rep(1,length(deviations))*ave
plot(xdata,SRR,col="blue",main="SRR vs its AVE",
    ylab="cycles per second", xlab ="index of data pt")+
points(xdata,aveVec,col="red")
sum(deviations)
mean(abs(deviations))
variance = sum((SRR - mean(SRR))^2)/ ( length(SRR) - 1 );variance
stddev = sqrt(variance);stddev
var(SRR)
sd(SRR)
prior=c(1.25, 2.94, 2.38, 3.09, 3.41, 3.00, 2.31, 2.93,
  2.98, 3.55, 2.84, 1.64, 3.22, 2.87, 2.37, 1.91)
after=c(2.40, 3.50, 4.49, 3.17, 5.26, 3.22, 2.32, 3.31,
  3.70, 4.70, 4.94, 5.06, 3.22, 3.52, 5.45, 3.40)
spider=data.frame(prior, after)
boxplot(spider)
plot(spider, xlim = c(0,6), ylim = c(0,6),
  xlab="speed prior to loss", ylab="speed after loss",
  main="male Tidarren spider study", sub = "pedipalp self-amputation",
  col.main="red", col.sub="blue", col.lab="green"
)

Day 3

SnakeRotationRates = c(0.9, 1.4, 1.2, 1.2, 1.3, 2.0, 1.4, 1.6)
hist(SnakeRotationRates,
  breaks=5,
  col="lightblue",
  xlab="cycles per second",
  main="Snake Rotation Rates"
)
sum(SnakeRotationRates)/length(SnakeRotationRates) #finds the average
mean(SnakeRotationRates) #built-in function for average
sort(SnakeRotationRates) #median is middle value or average of the 2 straddling the middle.
median(SnakeRotationRates) #built-in function
SnakeTable=table(SnakeRotationRates)
barplot( #facilitates visual location of a discrete mode
  SnakeTable,xlab="cycles per second",
  main="Snake Rotation Rates"
) 
Mode = function(x){ #Finds mode for discrete data
  ta = table(x)
  names(ta)[ta == max(ta)] #outputs list of values achieving max freq
} 
snakesMode = Mode(SnakeRotationRates); snakesMode
as.numeric(snakesMode) #changes strings to numbers
ModeC = function(x,numBreaks){ #Finds mode for grouped data
  h = hist(x, breaks=numBreaks)
  h$mids[h$counts == max(h$counts)]#outputs list of midpts achieving max
} 
ModeC(SnakeRotationRates,5)

Day 2

gearfreq = table(mtcars$gear)
cylfreq = table(mtcars$cyl)
vsfreq = table(mtcars$vs)
amfreq = table(mtcars$am)
carbfreq = table(mtcars$carb)
barplot(gearfreq, main = "gears for mtcars", xlab = "number")
barplot(gearfreq, main = "gears for mtcars", xlab = "number", horiz=TRUE)