Building a Two-Way Table

setwd("/Users/Greg/Documents/Grad School/LAIS 609C/Assignment 1")
load("nightlight.RData")
t = table(nightlight);t
##              Nearsightedness
## Light          No Yes
##   lamp         34  41
##   night light 153  79
##   no light    155  17
tt = table(nightlight$Light,nightlight$Nearsightedness);tt
##              
##                No Yes
##   lamp         34  41
##   night light 153  79
##   no light    155  17
prop.table(t,1)
##              Nearsightedness
## Light                 No        Yes
##   lamp        0.45333333 0.54666667
##   night light 0.65948276 0.34051724
##   no light    0.90116279 0.09883721
prop.table(t,2)
##              Nearsightedness
## Light                No       Yes
##   lamp        0.0994152 0.2992701
##   night light 0.4473684 0.5766423
##   no light    0.4532164 0.1240876
prop.table(t,1)*100
##              Nearsightedness
## Light                No       Yes
##   lamp        45.333333 54.666667
##   night light 65.948276 34.051724
##   no light    90.116279  9.883721

Creating a Scatterplot

load("height.RData")
plot(h$height,h$weight)

plot(h$height,h$weight, xlab="Height (inches)", ylab="Weight (lbs)")

plot(h$height,h$weight, xlab="Height (inches)", ylab="Weight (lbs)")
points(h$height[h$gender==1],h$weight[h$gender==1],col="red")

plot(h$height,h$weight, xlab="Height (inches)", ylab="Weight (lbs)",col=ifelse(h$gender==1,"red", "blue"))
legend(55,225, pch=1, col=c("red","blue"),legend=c("females","males"))

Computing a Correlation

load("animals.RData")
plot(a$longevity,a$gestation,xlab="Average Longevity of Species (years)", ylab="Average Gestation Period of Species (days)")

cor(a$longevity,a$gestation)
## [1] 0.6632397
cor(a$longevity[a$animal!="elephant"],a$gestation[a$animal!="elephant"])
## [1] 0.5190389

Finding a Regression Line

load("olympics_2012.RData")
plot(olym$Year, olym$Time, xlab="Year of Olympic Games",ylab="Winning Time of 1500m Race (secs)")

model = lm(olym$Time~olym$Year)
plot(olym$Year, olym$Time, xlab="Year of Olympic Games", ylab="Winning Time of 1500m Race (secs)");abline(model)

plot(olym$Year[olym$Year!=1896], olym$Time[olym$Year!=1896]) 
L = lm(olym$Time[olym$Year!=1896]~olym$Year[olym$Year!=1896]); 
abline(L);
cf=coefficients(L);
legend(1950,240,legend=paste("time = ",round(cf[1],0),round(cf[2],2),"year"))