R Markdown

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:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

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.

library(statsr)
## Warning: package 'statsr' was built under R version 4.0.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.2
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v tibble  3.0.4     v purrr   0.3.4
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## Warning: package 'tibble' was built under R version 4.0.3
## Warning: package 'tidyr' was built under R version 4.0.2
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
data(kobe_basket)
kobe_basket <- write.csv(kobe_basket, file = "kobe_basket.csv", row.names = FALSE)
#kobe_streak <- calc_streak(kobe_basket$shot)
#kobe_streak <- write.csv(kobe_streak, file = "kobe_streak.csv", row.names = FALSE)

When i tried to write.csv immediately after create kobe_streak, the file had null values. AFter I commented it out, the file

was created. Why?? I also lost the data when i ran write.csv in its own chunk. Why? But I do have the saved file in the Lab3 folder. This is screwy.

kobe_streak <- calc_streak(kobe_basket$shot)
#kobe_streak <- write.csv(kobe_streak, file = "kobe_streak.csv", row.names = FALSE)
ggplot (data = kobe_streak, aes(x = length)) +
   geom_histogram(binwidth = 1)

coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 1, replace = TRUE)
## [1] "tails"
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)
sim_fair_coin
##   [1] "heads" "heads" "tails" "heads" "heads" "tails" "heads" "tails" "heads"
##  [10] "heads" "heads" "tails" "heads" "heads" "heads" "tails" "heads" "heads"
##  [19] "heads" "tails" "heads" "heads" "heads" "heads" "tails" "tails" "tails"
##  [28] "tails" "heads" "tails" "tails" "heads" "heads" "tails" "heads" "heads"
##  [37] "heads" "tails" "tails" "tails" "tails" "tails" "heads" "tails" "heads"
##  [46] "heads" "tails" "heads" "heads" "tails" "heads" "tails" "tails" "tails"
##  [55] "tails" "tails" "heads" "heads" "tails" "heads" "tails" "heads" "heads"
##  [64] "tails" "heads" "heads" "heads" "tails" "tails" "heads" "tails" "tails"
##  [73] "tails" "tails" "heads" "heads" "tails" "heads" "tails" "heads" "heads"
##  [82] "heads" "tails" "heads" "tails" "heads" "heads" "heads" "tails" "heads"
##  [91] "tails" "heads" "tails" "heads" "heads" "tails" "tails" "tails" "heads"
## [100] "heads"
table(sim_fair_coin)
## sim_fair_coin
## heads tails 
##    55    45
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE,
prob = c(0.2, 0.8))
sim_unfair_coin
##   [1] "heads" "tails" "tails" "tails" "tails" "heads" "tails" "tails" "tails"
##  [10] "tails" "tails" "heads" "heads" "tails" "tails" "tails" "tails" "tails"
##  [19] "tails" "tails" "heads" "tails" "tails" "tails" "tails" "tails" "tails"
##  [28] "tails" "heads" "tails" "heads" "tails" "tails" "heads" "tails" "tails"
##  [37] "tails" "heads" "tails" "heads" "tails" "tails" "tails" "heads" "tails"
##  [46] "heads" "tails" "tails" "tails" "heads" "heads" "tails" "tails" "tails"
##  [55] "tails" "heads" "tails" "tails" "heads" "tails" "tails" "tails" "tails"
##  [64] "heads" "tails" "heads" "tails" "tails" "tails" "tails" "tails" "tails"
##  [73] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
##  [82] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
##  [91] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [100] "tails"
table(sim_unfair_coin)
## sim_unfair_coin
## heads tails 
##    18    82
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 1, replace = TRUE)
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
table(sim_basket)
## sim_basket
##  H  M 
## 52 81
sim_streak <- calc_streak(sim_basket)
ggplot (data = sim_streak, aes(x = length)) +
   geom_histogram(binwidth = 1)