This is my WPA
library("yarrr")
# C
names(capture)
## [1] "size" "cannons" "style" "warnshot"
## [5] "date" "heardof" "decorations" "daysfromshore"
## [9] "speed" "treasure"
# D
head(capture)
## size cannons style warnshot date heardof decorations daysfromshore
## 1 48 54 classic 0 172 1 8 28
## 2 51 56 modern 0 15 0 3 6
## 3 50 44 modern 0 63 0 3 23
## 4 54 54 modern 0 362 1 2 23
## 5 50 56 modern 0 183 1 2 12
## 6 51 48 modern 0 279 0 1 3
## speed treasure
## 1 16 2175
## 2 29 2465
## 3 18 1925
## 4 19 2200
## 5 21 2290
## 6 24 2195
# A
object <- lm(treasure ~ size, data=capture)
with(capture, plot(size, treasure))
abline(object, col="blue")
# B
object2 <- lm(treasure ~ cannons, data=capture)
with(capture, plot(cannons, treasure))
abline(object2, col="blue")
# C
object3 <- lm(treasure ~ date, data=capture)
with(capture, plot(date, treasure))
abline(object3, col="blue")
# D
object4 <- lm(treasure ~ decorations, data=capture)
with(capture, plot(decorations, treasure))
abline(object4, col="blue")
# E
object5 <- lm(treasure ~ daysfromshore, data=capture)
with(capture, plot(daysfromshore, treasure))
abline(object5, col="blue")
# F
object6 <- lm(treasure ~ speed, data=capture)
with(capture, plot(speed, treasure))
abline(object6, col="blue")
#A
A2 <- lm(treasure ~ style, data=capture)
pirateplot(dv.name = "treasure",
iv.name = "style",
data = capture,
add.hdi = F)
abline(A2, col="red")
#B
B2 <- lm(treasure ~ warnshot, data=capture)
pirateplot(dv.name = "treasure",
iv.name = "warnshot",
data = capture,
add.hdi = F)
abline(B2, col="red")
#C
C2 <- lm(treasure ~ heardof, data=capture)
pirateplot(dv.name = "treasure",
iv.name = "heardof",
data = capture,
add.hdi = F)
abline(C2, col="red")
#For each of the following variables (separately), calculate the median amount of treasure earned for each level of the IV: style, warnshot, decorations (hint: use aggregate or dplyr!)
with(capture, aggregate(treasure ~ style, FUN = mean))
## style treasure
## 1 classic 2184.301
## 2 modern 2095.645
with(capture, aggregate(treasure ~ warnshot, FUN = mean))
## warnshot treasure
## 1 0 2085.940
## 2 1 2174.802
with(capture, aggregate(treasure ~ decorations, FUN = mean))
## decorations treasure
## 1 1 3175.472
## 2 2 1764.758
## 3 3 1865.688
## 4 4 1847.904
## 5 5 1881.486
## 6 6 1879.208
## 7 7 1954.426
## 8 8 1998.511
## 9 9 1962.857
## 10 10 2005.526
#The formula notation for conducting a correlation test with cor.test() is a bit different from regular formula notation. Instead of dv ~ iv, you use ~ dv + iv. For example, the following code will test the correlation between chickens’ age and weight using the ChickWeight dataset.
# A. Using the formula notation above, conduct a correlation test between the number of cannons a ship has and its size. What is the p-value?
cor.test(~ cannons + size,
data = capture)
##
## Pearson's product-moment correlation
##
## data: cannons and size
## t = 0.90501, df = 998, p-value = 0.3657
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.03341657 0.09046832
## sample estimates:
## cor
## 0.02863584
# p-value = 0.3657
# B. Now do the same with linear regression. What is the p-value?
can.ship <- lm(size ~ cannons,
data = capture)
summary(can.ship)
##
## Call:
## lm(formula = size ~ cannons, data = capture)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.9844 -2.8873 -0.0596 2.9028 13.1409
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 49.859132 0.262458 189.970 <2e-16 ***
## cannons 0.006266 0.006923 0.905 0.366
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.14 on 998 degrees of freedom
## Multiple R-squared: 0.00082, Adjusted R-squared: -0.0001812
## F-statistic: 0.819 on 1 and 998 DF, p-value: 0.3657
# Conduct a linear regression with treasure as the dependent variable, and with all other variables as independent variables. Save the object as treasure.model
treasure.model <- lm(treasure ~ size + cannons + style + warnshot + date + heardof + decorations + daysfromshore + speed + treasure,
data = capture)
## Warning in model.matrix.default(mt, mf, contrasts): the response appeared
## on the right-hand side and was dropped
## Warning in model.matrix.default(mt, mf, contrasts): problem with term 10 in
## model.matrix: no columns are assigned
#Using the summary() function, print the coefficients and main statistics of the regression
summary(treasure.model)
##
## Call:
## lm(formula = treasure ~ size + cannons + style + warnshot + date +
## heardof + decorations + daysfromshore + speed + treasure,
## data = capture)
##
## Residuals:
## Min 1Q Median 3Q Max
## -880.96 -443.16 -211.02 66.08 2427.97
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 749.8957 351.0514 2.136 0.032913 *
## size 22.5203 5.9602 3.778 0.000167 ***
## cannons 19.3817 1.2932 14.987 < 2e-16 ***
## stylemodern -165.0932 84.6314 -1.951 0.051371 .
## warnshot 89.0164 61.0610 1.458 0.145205
## date 0.1508 0.2313 0.652 0.514511
## heardof 92.1270 54.7238 1.683 0.092595 .
## decorations -96.3998 10.0249 -9.616 < 2e-16 ***
## daysfromshore -8.6119 2.8180 -3.056 0.002303 **
## speed 9.2639 8.3892 1.104 0.269750
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 771.4 on 990 degrees of freedom
## Multiple R-squared: 0.2661, Adjusted R-squared: 0.2594
## F-statistic: 39.88 on 9 and 990 DF, p-value: < 2.2e-16