The purpose of this R lab is to have you practice testing assumptions, interpretting output, practicing ways to deal with violations of assumptions and to dip into regression and correlation
NOTE: please refer to the hypothesis testing guide document for code examples!
Create a code chunk(s) below to bring in “barnacle_data.csv” and complete the following actions. There are questions that will follow your coding. This data comes from some of my research looking at how things like microhabitat and shore height influence the growth and abundance of barnacles.
Actions/Questions: You are interested in seeing if there is an effect of shore height (‘Tide.Height’) on the growth of individual barnacles over a 6 month period.
barnacle <- read.csv("barnacle_data_2.csv", header = TRUE)
str(barnacle)
## 'data.frame': 191 obs. of 4 variables:
## $ Tide.Height : chr "H" "H" "H" "H" ...
## $ OperculumAug: num 5.26 5.62 5.48 5.07 5.23 ...
## $ BasalAug : num 7.1 9.26 8.29 11.58 9.56 ...
## $ WidthChange : num 68.019 18.658 0.977 6.366 12.214 ...
table(barnacle$Tide.Height)
##
## H L
## 94 97
hist(barnacle$WidthChange)
shapiro.test(barnacle$WidthChange)
##
## Shapiro-Wilk normality test
##
## data: barnacle$WidthChange
## W = 0.4299, p-value < 2.2e-16
library(car)
## Loading required package: carData
leveneTest(WidthChange ~ Tide.Height,
data = barnacle)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 5.1557 0.0243 *
## 189
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
barnacle$logWidthChange <- log(barnacle$WidthChange)
str(barnacle)
## 'data.frame': 191 obs. of 5 variables:
## $ Tide.Height : chr "H" "H" "H" "H" ...
## $ OperculumAug : num 5.26 5.62 5.48 5.07 5.23 ...
## $ BasalAug : num 7.1 9.26 8.29 11.58 9.56 ...
## $ WidthChange : num 68.019 18.658 0.977 6.366 12.214 ...
## $ logWidthChange: num 4.2198 2.9263 -0.0231 1.8509 2.5026 ...
shapiro.test(barnacle$logWidthChange)
##
## Shapiro-Wilk normality test
##
## data: barnacle$logWidthChange
## W = 0.95978, p-value = 2.894e-05
leveneTest(logWidthChange ~ Tide.Height,
data = barnacle)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.225 0.6358
## 189
wilcox.test(WidthChange ~ Tide.Height, data = barnacle)
##
## Wilcoxon rank sum test with continuity correction
##
## data: WidthChange by Tide.Height
## W = 2200, p-value = 6.623e-10
## alternative hypothesis: true location shift is not equal to 0
boxplot(WidthChange ~ Tide.Height,
data = barnacle,
main = "Barnacle Width Change by Tide Height",
xlab = "Tide Height",
ylab = "Width Change",
col = c("blue4", "lightblue"))
The barnacle data set had measured key size metric for this species over time. In Part 1, you explored how tide height would impact the growht of one of these metrics, basal width. However, we collected two types of size data on the barnacles, their basal width and their operculum length. Much like we see body and brain size correlate, do we see basal width and operculum length do the same thing?
Actions/Questions: Complete the following using one or several code chunks with the ‘barnacle_data_2.csv’ file
plot(x = barnacle$BasalAug,
y = barnacle$OperculumAug,
pch = 16,
col = "blue",
xlab = "Basal Width",
ylab = "Operculum Length",
main = "Operculum Length vs. Basal Width")
2. I am expecting a strong positive correlation because there seems to
be a somewhat clear trend in the data from the scatter plot. 3.
Correlation Test
cor(x = barnacle$BasalAug, y = barnacle$OperculumAug)
## [1] 0.7152863
cor.test(x = barnacle$BasalAug, y = barnacle$OperculumAug, alternative = "two.sided")
##
## Pearson's product-moment correlation
##
## data: barnacle$BasalAug and barnacle$OperculumAug
## t = 14.071, df = 189, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6381109 0.7782316
## sample estimates:
## cor
## 0.7152863
Load the data ‘environmental.csv’, this dataset has tracked environmental parameters in NYC throughout the course of a summer. You are interested developing a predictive model that tracks how radiation influences temperature. This would allow you to better hone in on weather forecasting.
Actions/Questions: Complete the following using one or several code chunks
enviro <- read.csv("environmental.csv", header = TRUE)
str(enviro)
## 'data.frame': 111 obs. of 5 variables:
## $ rownames : int 1 2 3 4 5 6 7 8 9 10 ...
## $ ozone : int 41 36 12 18 23 19 8 16 11 14 ...
## $ radiation : int 190 118 149 313 299 99 19 256 290 274 ...
## $ temperature: int 67 72 74 62 65 59 61 69 66 68 ...
## $ wind : num 7.4 8 12.6 11.5 8.6 13.8 20.1 9.7 9.2 10.9 ...
plot(x = enviro$radiation,
y = enviro$temperature,
pch = 16,
col = "black",
xlab = "Radiation",
ylab = "Temperature",
main = "Relationship between Radiation and Temperature")
linreg <- lm(temperature ~ radiation,
data = enviro)
summary(linreg)
##
## Call:
## lm(formula = temperature ~ radiation, data = enviro)
##
## Residuals:
## Min 1Q Median 3Q Max
## -19.735 -6.292 1.080 6.231 18.648
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 72.110720 1.970502 36.595 < 2e-16 ***
## radiation 0.030747 0.009571 3.212 0.00173 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.15 on 109 degrees of freedom
## Multiple R-squared: 0.08649, Adjusted R-squared: 0.07811
## F-statistic: 10.32 on 1 and 109 DF, p-value: 0.001731
plot(enviro$radiation,
enviro$temperature,
main = "Temperature vs. Radiation",
xlab = "Radiation",
ylab = "Temperature",
pch = 19)
abline(linreg,
col = "red",
lwd = 2)
new_radiation <- data.frame(radiation = 180)
predict(linreg,
newdata = new_radiation)
## 1
## 77.64515