install.packages(“devtools”) library(“devtools”) install_github(“ndphillips/yarrr”) library(“yarrr”)
capture <- read.table("http://nathanieldphillips.com/wp-content/uploads/2015/12/capture.txt",
sep = "\t",
header = T)
??capture
# Q0
#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
# Q1
# A
plot(x = capture$size,
y = capture$treasure,
xlab = "Ship size",
ylab = "treasure in the ship",
main = "Relationship between size and treasure")
size.treasure.lm <- lm(treasure~ size,
data = capture)
abline(size.treasure.lm,
lty = 1,
lwd = 2,
col = "coral")
# B
plot(x = capture$cannons,
y = capture$treasure,
xlab = "cannons",
ylab = "treasure in the ship",
main = "Relationship between cannons and treasure")
cannons.treasure.lm <- lm(treasure~ cannons,
data = capture)
abline(cannons.treasure.lm,
lty = 1,
lwd = 2,
col = "blue")
# C
plot(x = capture$date,
y = capture$treasure,
xlab = "date",
ylab = "treasure in the ship",
main = "Relationship between date and treasure")
date.treasure.lm <- lm(treasure~ date,
data = capture)
abline(date.treasure.lm,
lty = 1,
lwd = 2,
col = "red")
# D
plot(x = capture$decorations,
y = capture$treasure,
xlab = "decorations",
ylab = "treasure in the ship",
main = "Relationship between decorations and treasure")
decorations.treasure.lm <- lm(treasure~ decorations,
data = capture)
abline(decorations.treasure.lm,
lty = 1,
lwd = 2,
col = "green")
# E
plot(x = capture$daysfromshore,
y = capture$treasure,
xlab = "daysfromshore",
ylab = "treasure in the ship",
main = "Relationship between daysfromshore and treasure")
daysfromshore.treasure.lm <- lm(treasure~ daysfromshore,
data = capture)
abline(daysfromshore.treasure.lm,
lty = 1,
lwd = 2,
col = "orange")
# F
plot(x = capture$speed,
y = capture$treasure,
xlab = "speed",
ylab = "treasure in the ship",
main = "Relationship between speed and treasure")
speed.treasure.lm <- lm(treasure~ speed,
data = capture)
abline(speed.treasure.lm,
lty = 1,
lwd = 2,
col = "violet")
# Q2
require(beanplot)
## Loading required package: beanplot
# A
beanplot(treasure ~ style,
data = capture,
col = "blue",
ylab = "treasure",
xlab = "style",
main = "treasure by style"
)
## log="y" selected
style.treasure.lm <- lm(treasure~ style,
data = capture)
abline(style.treasure.lm,
lty = 1,
lwd = 2,
col = "violet")
# B
beanplot(treasure ~ warnshot,
data = capture,
col = "blue",
ylab = "treasure",
xlab = "warnshot",
main = "treasure by warnshot"
)
## log="y" selected
warnshot.treasure.lm <- lm(treasure~ warnshot,
data = capture)
abline(warnshot.treasure.lm,
lty = 1,
lwd = 2,
col = "blue")
# C
beanplot(treasure ~ heardof,
data = capture,
col = "blue",
ylab = "treasure",
xlab = "heardof",
main = "treasure by heardof"
)
## log="y" selected
heardof.treasure.lm <- lm(treasure~ heardof,
data = capture)
abline(heardof.treasure.lm,
lty = 1,
lwd = 2,
col = "orange")
# Q3
# A
style.treasure.median <- aggregate(formula = treasure ~ style,
FUN = median,
na.rm = T,
data = capture)
style.treasure.median
## style treasure
## 1 classic 2000
## 2 modern 1895
# B
warnshot.treasure.median <- aggregate(formula = treasure ~ warnshot,
FUN = median,
na.rm = T,
data = capture)
warnshot.treasure.median
## warnshot treasure
## 1 0 1885
## 2 1 1945
# C
decorations.treasure.median <- aggregate(formula = treasure ~ decorations,
FUN = median,
na.rm = T,
data = capture)
decorations.treasure.median
## decorations treasure
## 1 1 2657.5
## 2 2 1780.0
## 3 3 1905.0
## 4 4 1797.5
## 5 5 1880.0
## 6 6 1855.0
## 7 7 1920.0
## 8 8 1935.0
## 9 9 1935.0
## 10 10 1955.0
# Q4
# A
cor.test(~ size + cannons,
data = capture)
##
## Pearson's product-moment correlation
##
## data: size and cannons
## 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
# B
size.cannons.lm <- lm(size ~ cannons,
data = capture)
size.cannons.lm
##
## Call:
## lm(formula = size ~ cannons, data = capture)
##
## Coefficients:
## (Intercept) cannons
## 49.859132 0.006266
summary(size.cannons.lm)
##
## 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
# Q5
# A
treasure.model <- lm(treasure ~ .,
data = capture)
treasure.model
##
## Call:
## lm(formula = treasure ~ ., data = capture)
##
## Coefficients:
## (Intercept) size cannons stylemodern warnshot
## 749.8957 22.5203 19.3817 -165.0932 89.0164
## date heardof decorations daysfromshore speed
## 0.1508 92.1270 -96.3998 -8.6119 9.2639
# B
summary(treasure.model)
##
## Call:
## lm(formula = 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