library(Stat2Data) library(ggplot2) #------------------------------------------------------------------------ #Example 1: Can skull length predict overall body mass in blue jays? #Get data data(BlueJays) #Test Assumptions #Independence #depends on sample design and taken to be valid here #Normality hist(BlueJays$Mass, col='blue') #good #Linear Relationship and Homeoscedasticity plot(Mass~Skull, data=BlueJays, col='blue') #looks good for both lm1 <- lm(Mass~Skull, data=BlueJays) plot(lm1, col="blue") #looks good for both #Simple Linear Regression lm1 <- lm(Mass~Skull, data=BlueJays) summary(lm1) #B=2.88, SE=0.3941, t=7.30, p<0.0001 #Graph plot1 <- ggplot(data=BlueJays, aes(x=Skull, y=Mass)) + geom_point() + geom_smooth(method=lm, color="blue") plot1 #------------------------------------------------------------------------ #Example 2: Can resting pulse rate predict active pulse rate in statistics students? #Test Assumptions #Get data data(Pulse) #Test Assumptions #Independence #depends on sample design and taken to be valid here #Normality hist(Pulse$Active, col='red') #little skewed but good enough #Linear Relationship and Homeoscedasticity plot(Active~Rest, data=Pulse, col='red') #looks good for both lm2 <- lm(Active~Rest, data=Pulse) plot(lm2, col="red") #looks good enough for both #Simple Linear Regression lm2 <- lm(Active~Rest, data=Pulse) summary(lm2) #B=1.14, SE=0.10, t=11.50, p<0.0001 #Graph plot2 <- ggplot(data=Pulse, aes(x=Rest, y=Active)) + geom_point() + geom_smooth(method=lm, color="Red") plot2 #------------------------------------------------------------------------ #Example 3: Can sample depth predict iridium concentration in prehistoric rock layers? #Test Assumptions #Get Data data(Dinosaurs) #Independence #depends on sample design and taken to be valid here #Normality hist(Dinosaurs$Iridium, col='forestgreen') #not normally distributed hist(log(Dinosaurs$Iridium), col='forestgreen') #still not normally distributed (try anyways) #Linear Relationship and Homeoscedasticity plot(Iridium~Depth, data=Dinosaurs, col='forestgreen') #not linear lm3 <- lm(Iridium~Depth, data=Dinosaurs) plot(lm3, col="forestgreen") #not linear, may also be heteroscedastic (try anyways) #Simple Linear Regression lm3 <- lm(Iridium~Depth, data=Dinosaurs) summary(lm3) #B=-35.94, SE=23.71, t=-1.52, p=0.1420 #Graph plot3 <- ggplot(data=Dinosaurs, aes(x=Depth, y=Iridium)) + geom_point() + geom_smooth(method=lm, color="forestgreen") plot3