This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
capture <-read.table("http://nathanieldphillips.com/wp-content/uploads/2015/12/capture.txt")
```
names(capture)
## [1] "size" "cannons" "style" "warnshot"
## [5] "date" "heardof" "decorations" "daysfromshore"
## [9] "speed" "treasure"
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
size.treasure <- lm( formula = treasure ~ size, data = capture)
plot(x = capture$size,
y = capture$treasure,
xlab = "size",
ylab = "treasure",
main = "Relationship between treasure and size")
abline(size.treasure)
cannons.treasure <- lm(treasure ~ cannons, data = capture)
plot(x = capture$cannons,
y = capture$treasure,
xlab = "cannons",
ylab = "treasure",
main = "Relationship between treasure and cannons")
abline(cannons.treasure)
date.treasure <- lm(treasure ~ date, data = capture)
plot(x = capture$date,
y = capture$treasure,
xlab = "date",
ylab = "treasure",
main = "Relationship between treasure and date")
abline(date.treasure)
decorations.treasure <- lm(treasure ~ decorations, data = capture)
plot(x = capture$decorations,
y = capture$treasure,
xlab = "decorations",
ylab = "treasure",
main = "Relationship between treasure and decorations")
abline(decorations.treasure)
daysfromshore.treasure <- lm(treasure ~ daysfromshore, data = capture)
plot(x = capture$daysfromshore,
y = capture$treasure,
xlab = "daysfromshore",
ylab = "treasure",
main = "Relationship between treasure and days from shore")
abline(daysfromshore.treasure)
speed.treasure <- lm(treasure ~ speed, data = capture)
plot(x = capture$speed,
y = capture$treasure,
xlab = "speed",
ylab = "treasure",
main = "Relationship between treasure and speed")
abline(speed.treasure)
treasure.warnshot.glm <- glm(warnshot ~ treasure,
data = capture,
family = "binomial")
capture.warnshot <- capture$warnshot == "1"
capture$treasure.cut <- cut(capture$treasure,
breaks = seq(1000, 5000, 1000))
probs <- aggregate(capture.warnshot ~ treasure.cut,
data = capture, FUN = mean)
plot(probs, xlab = "treasure (grouped)",
ylab = "p(ship fired a warnshot)",
main = "Probabiliy that a ship fired a warnshot given its amount of treasure")
treasure.heardof.glm <- glm(heardof ~ treasure,
data = capture,
family = "binomial")
capture.heardof <- capture$heardof == "1"
capture$treasure.cut <- cut(capture$treasure,
breaks = seq(1000, 5000, 1000))
probs <- aggregate(capture.heardof ~ treasure.cut,
data = capture, FUN = mean)
plot(probs, xlab = "treasure grouped", ylab = "p ship was heard of",
main = "Amount of treasure in relation to the probability that you have heard of that ship")
``` ###Q3 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!)
aggregate (treasure ~ capture.heardof, data = capture, FUN = median)
## capture.heardof treasure
## 1 FALSE 1875
## 2 TRUE 1940
aggregate (treasure ~ capture.warnshot, data = capture, FUN = median)
## capture.warnshot treasure
## 1 FALSE 1885
## 2 TRUE 1945
aggregate (treasure ~ decorations, data = capture, FUN = 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
cor.test(~ Time + weight,
data = ChickWeight)
##
## Pearson's product-moment correlation
##
## data: Time and weight
## t = 36.725, df = 576, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.8109073 0.8599481
## sample estimates:
## cor
## 0.8371017
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
cannon.lm <- lm(cannons ~ size, data = capture)
summary(cannon.lm)
##
## Call:
## lm(formula = cannons ~ size, data = capture)
##
## Residuals:
## Min 1Q Median 3Q Max
## -34.549 -14.324 -0.324 12.498 63.414
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 26.3039 7.2645 3.621 0.000308 ***
## size 0.1309 0.1446 0.905 0.365679
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 18.92 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
treasure.model<-lm (treasure ~ size + cannons + warnshot + date + heardof+ decorations + daysfromshore + speed, data = capture)
summary(treasure.model)
##
## Call:
## lm(formula = treasure ~ size + cannons + warnshot + date + heardof +
## decorations + daysfromshore + speed, data = capture)
##
## Residuals:
## Min 1Q Median 3Q Max
## -760.81 -451.21 -211.49 58.56 2423.42
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 604.0837 343.4865 1.759 0.078940 .
## size 22.5769 5.9685 3.783 0.000164 ***
## cannons 19.3464 1.2949 14.940 < 2e-16 ***
## warnshot 82.4122 61.0533 1.350 0.177375
## date 0.1328 0.2315 0.574 0.566326
## heardof 98.4881 54.7038 1.800 0.072104 .
## decorations -95.0941 10.0166 -9.494 < 2e-16 ***
## daysfromshore -8.4173 2.8202 -2.985 0.002909 **
## speed 8.7189 8.3965 1.038 0.299338
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 772.5 on 991 degrees of freedom
## Multiple R-squared: 0.2633, Adjusted R-squared: 0.2573
## F-statistic: 44.27 on 8 and 991 DF, p-value: < 2.2e-16
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.