Quotes help make search much faster. Example: "Practice Makes Perfect"

Showing posts with label Learn R. Show all posts
Showing posts with label Learn R. Show all posts

Monday, September 9, 2019

Codecademy Hypothesis Testing With R ANOVA 13/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r message = FALSE}
# load data
load("dist_one.Rda")
load("dist_two.Rda")
load("dist_three.Rda")
load("dist_four.Rda")
```

```{r}
# plot histograms and define not_normal here:
hist(dist_one)
hist(dist_two)
hist(dist_three)
hist(dist_four)
not_normal <- 4
```

```{r}
# define ratio here:
ratio <- sd(dist_two)/sd(dist_three)
ratio
```

Codecademy Hypothesis Testing With R ANOVA 12/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r message = FALSE}
# load libraries
library(tidyr)
```

```{r message = FALSE}
# load data
load("stores.Rda")
load("stores_new.Rda")
```

```{r}
# inspect stores here:
stores
```

```{r}
# perform anova on stores here:
results = aov(sales ~ store, data = stores)
summary(results)
```

```{r}
# perform anova on stores_new here:
results_new = aov(sales ~ store, data = stores_new)
summary(results_new)
```

Codecademy Hypothesis Testing With R Dangers of Multiple T-Tests 11/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r message = FALSE}
# load data
load("store_a.Rda")
load("store_b.Rda")
load("store_c.Rda")
```

```{r}
# calculate means here:
store_a_mean <- mean(store_a)
store_a_mean
store_b_mean <- mean(store_b)
store_b_mean
store_c_mean <- mean(store_c)
store_c_mean
```

```{r}
# calculate standard deviations here:
store_a_sd <- sd(store_a)
store_a_sd
store_b_sd <- sd(store_b)
store_b_sd
store_c_sd <- sd(store_c)
store_c_sd
```

```{r}
# perform two sample t-test here:
a_b_results <- t.test(store_a,store_b)
a_b_results
a_c_results <- t.test(store_a,store_c)
a_c_results
b_c_results <- t.test(store_b,store_c)
b_c_results
```

```{r}
# calculate error_prob here:
error_prob <- (1-(0.95**3))
error_prob
```

Codecademy Hypothesis Testing With R Two-Sample T-Test 10/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r message = FALSE}
# load data
load("week_1.Rda")
week_1
load("week_2.Rda")
week_2
```

```{r}
# calculate week_1_mean and week_2_mean here:
week_1_mean <- mean(week_1)
week_1_mean
week_2_mean <- mean(week_2)
week_2_mean
```

```{r}
# calculate week_1_sd and week_2_sd here:
week_1_sd <- sd(week_1)
week_1_sd
week_2_sd <- sd(week_2)
week_2_sd
```

```{r}
# run two sample t-test here:
results <- t.test(week_1,week_2)
results
```

Codecademy Hypothesis Testing With R One-Sample T-Test 9/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r message = FALSE}
# load and view data
load("ages.Rda")
ages
```

```{r}
# calculate ages_mean here:
ages_mean <- mean(ages)
ages_mean
```

```{r}
# perform t-test here:
results <- t.test(ages,mu=30)
results
```

Codecademy Hypothesis Testing With R Significance Level 8/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r}
# update reject_hypothesis here:
# all p-values below the threshold will result in # # rejecting the null hypothesis
# p-value threshold industry = 0.05
# hypothesis test actual p-value = 0.1 > 0.05
# since p-value is larger than the threshold, we # accept the null hypothesis
reject_hypothesis <- FALSE
```

Codecademy Hypothesis Testing With R P-Values 7/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r}
# possible interpretations
st_1 <- "There is a 20% chance that the difference in average weight of green and red apples is due to random sampling."
st_2 <- "There is a 20% chance that green and red apples have the same average weight."
st_3 <- "There is a 20% chance red apples weigh more than green apples."
st_4 <- "There is a 20% chance green apples weigh more than green apples."

# update the value of interpretation here:
interpretation <- "st_1"
```

Codecademy Hypothesis Testing With R Type I and Type II Errors 6/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r}
# the true positives and negatives:
actual_positive <- c(2, 5, 6, 7, 8, 10, 18, 21, 24, 25, 29, 30, 32, 33, 38, 39, 42, 44, 45, 47)
actual_negative <- c(1, 3, 4, 9, 11, 12, 13, 14, 15, 16, 17, 19, 20, 22, 23, 26, 27, 28, 31, 34, 35, 36, 37, 40, 41, 43, 46, 48, 49)

# the positives and negatives we determine by running the experiment:
experimental_positive <- c(2, 4, 5, 7, 8, 9, 10, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 24, 26, 27, 28, 32, 35, 36, 38, 39, 40, 45, 46, 49)
experimental_negative <- c(1, 3, 6, 12, 14, 23, 25, 29, 30, 31, 33, 34, 37, 41, 42, 43, 44, 47, 48)
```

```{r}
# define type_i_errors and type_ii_errors here:
type_i_errors = intersect(experimental_positive,actual_negative)
type_i_errors
type_ii_errors =
intersect(experimental_negative,actual_positive)
type_ii_errors
```

Codecademy Hypothesis Testing With R Designing an Experiment 5/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r}
# load data
load("retriever_lengths.Rda")
load("doodle_lengths.Rda")
```

```{r}
# calculate mean_retriever_l and mean_doodle_l here:
mean_retriever_l = mean(retriever_lengths)
mean_doodle_l = mean(doodle_lengths)
mean_retriever_l
mean_doodle_l
```

```{r}
# calculate mean_difference here:
mean_difference = mean_retriever_l - mean_doodle_l
mean_difference
```

```{r}
# statements:
st_1 <- "The average length of Golden Retrievers is 2.5 inches longer than the average length of Goldendoodles."
st_2 <- "The average length of Golden Retrievers is the same as the average length of Goldendoodles."

# update null_hypo here:
null_hypo <- "st_2"
```

Saturday, September 7, 2019

Codecademy Hypothesis Testing With R Hypothesis Formulation 4/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r}
# experiment 1
hypo_a <- "DeePressurize lowers blood pressure in patients."
hypo_b <- "DeePressurize has no effect on blood pressure in patients."
null_hypo_1 <- "hypo_b"
```

```{r}
# experiment 2
hypo_c <- "The new profile layout has no effect on number of matches with other users."
hypo_d <- "The new profile layout results in more matches with other users than the original layout."
null_hypo_2 <- "hypo_c"
```

Codecademy Hypothesis Testing With R Sample Mean and Population Mean - I 2/14

#Sample Solution
---
title: "Hypothesis Testing in R"
output: html_notebook
---

```{r}
# generate random population
population <- rnorm(300, mean=65, sd=3.5)

# calculate population mean here:
population_mean <- mean(population)
population_mean
```

```{r}
# generate sample 1
sample_1 <- sample(population, size=30)
sample_1

# calculate sample 1 mean
sample_1_mean <- mean(sample_1)
sample_1_mean
```

```{r}
# generate samples 2,3,4 and 5
sample_2 <- sample(population, size=30)
sample_3 <- sample(population, size=30)
sample_4 <- sample(population, size=30)
sample_5 <- sample(population, size=30)
```

```{r}
# calculate sample means here:
sample_2_mean <- mean(sample_2)
sample_2_mean
sample_3_mean <- mean(sample_3)
sample_3_mean
sample_4_mean <- mean(sample_4)
sample_4_mean
sample_5_mean <- mean(sample_5)
sample_5_mean
```

Codecademy Interquartile Range IQR in R 3/4

Codecademy Interquartile Range IQR in R 3/4
#Sample Solution
---
title: "Interquartile Range"
output: html_notebook
---

```{r}
# load song data
load("songs.Rda")
```

```{r}
# create the variable interquartile_range here
interquartile_range = IQR(songs)
```

```{r}
# ignore the code below here:

tryCatch(print(paste("The IQR of the dataset is",interquartile_range)), error=function(e) {print("You haven't defined interquartile_range yet")})
```

Codecademy Interquartile Range Quartiles 2/4

#Sample Solution
---
title: "Interquartile Range"
output: html_notebook
---

```{r}
# load song data
load("songs.Rda")
```

```{r}
# find the first quartile
q1 <- quantile(songs,0.25)
```

```{r}
# calculate the third quartile here:
q3 <- quantile(songs,0.75)
```

```{r}
# calculate the interquartile range here:
interquartile_range <- q3 - q1
```

```{r}
# ignore the code below here:

tryCatch(print(paste("The first quartile of the dataset is",q1)), error=function(e) {print("You haven't defined q1 yet")})

tryCatch(print(paste("The third quartile of the dataset is",q3)), error=function(e) {print("You haven't defined q3 yet")})

tryCatch(print(paste("The IQR of the dataset is",interquartile_range)), error=function(e) {print("You haven't defined interquartile_range yet")})
```

Codecademy Interquartile Range Range Review 1/4

#Sample Solution
---
title: "Interquartile Range"
output: html_notebook
---

```{r message=FALSE, warning=FALSE}
# load libraries
library(ggplot2)
```

```{r}
# load song data
load("songs.Rda")
```

```{r}
# find maximum and minimum song lengths
maximum <- max(songs)
minimum <- min(songs)
```

```{r}
# create variable song_range here:
song_range <- maximum - minimum
```

```{r message=FALSE, echo=FALSE}
# plot histogram
hist <- qplot(songs,
              geom="histogram",
              main = 'Histogram of Song Lengths',
              xlab = 'Song Length (Seconds)',
              ylab = 'Count',
              fill=I("blue"),
              col=I("red"),
              alpha=I(.2))
hist
```

```{r}
# ignore the code below here:

tryCatch(print(paste("The range of the dataset is",song_range)), error=function(e) {print("You haven't defined the variable song_range yet")})
```

Friday, September 6, 2019

Codecademy Quantiles Common Quantiles 4/5

#Sample Solution
---
title: "Quantiles"
output: html_notebook
---

```{r}
# load song data
load("songs.Rda")
```

```{r}
# define percentile and answer here:
percentile <- quantile(songs,0.32)
answer <- "below"
```

```{r}
# ignore the code below here:

tryCatch(print(paste("Your percentile is",percentile)), error=function(e) {print("You haven't defined percentile")})
```

Codecademy Quantiles Many Quantiles 3/5

#Sample Solution
---
title: "Quantiles"
output: html_notebook
---

```{r}
# load song data
load("songs.Rda")
```

```{r}
# define quartiles and deciles here:
quartiles <- quantile(songs, c(0.25, 0.5, 0.75))
decile_array <- c()
for(k in 1:9){
  decile_array[k]<-k/10
}
decile_array
deciles <- quantile(songs, decile_array)
deciles
```

```{r}
# define tenth here:
tenth <- 3
```

```{r}
# ignore the code below here:

tryCatch(print(paste(c("The quartiles are",quartiles,collapse=" "))), error=function(e) {print("You haven't defined quartiles.")})

tryCatch(print(paste(c("The deciles are",deciles,collapse=" "))), error=function(e) {print("You haven't defined deciles.")})
```

Codecademy Quantiles in R 2/5

#Sample Solution
---
title: "Quantiles"
output: html_notebook
---

```{r}
# load song data
load("songs.Rda")
```

```{r}
# define twenty_third_percentile here:
twenty_third_percentile <- quantile(songs,0.23)
```

```{r}
# ignore the code below here:

tryCatch(print(paste("The value that splits 23% of the data is",twenty_third_percentile)), error=function(e) {print("You haven't defined twenty_third_percentile.")})
```

Thursday, September 5, 2019

Codecademy Quartiles Quartiles in R 5/6

#Sample Solution
---
title: "Quartiles"
output: html_notebook
---

```{r}
# load song data
load("songs.Rda")
songs
```

```{r}
# create the variables songs_q1, songs_q2, and songs_q3 here:
songs_q1 <- quantile(songs, 0.25)
songs_q2 <- quantile(songs, 0.50)
songs_q3 <- quantile(songs, 0.75)
```

```{r}
# create the variables favorite_song and quarter here:
favorite_song <- 209.13587
quarter <- 2
```

```{r}
# ignore the code below here:

tryCatch(print(paste("The first quartile of dataset one is",songs_q1)), error=function(e) {print("You haven't defined songs_q1")})

tryCatch(print(paste("The second quartile of dataset two is",songs_q2)), error=function(e) {print("You haven't defined songs_q2")})

tryCatch(print(paste("The third quartile of dataset one is",songs_q3)), error=function(e) {print("You haven't defined songs_q3")})
```

Codecademy Quartiles Method Two: Including Q2 4/6

#Sample Solution
---
title: "Quartiles"
output: html_notebook
---

```{r}
dataset_one <- c(50, 10, 4, -3, 4, -20, 2)
# sorted dataset_one: [-20, -3, 2, 4, 4, 10, 50]

dataset_two <- c(24, 20, 1, 45, -15, 40)
# sorted dataset_two: c(-15, 1, 20, 24, 40, 45)

dataset_one_q2 <- 4
dataset_two_q2 <- 22

# define the first and third quartile of both datasets here:
dataset_one_q1 <- -0.5
dataset_one_q3 <- 7
dataset_two_q1 <- 1
dataset_two_q3 <- 40
```

```{r}
# ignore the code below here:

tryCatch(print(paste("The first quartile of dataset one is",dataset_one_q1)), error=function(e) {print("You haven't defined dataset_one_q1")})

tryCatch(print(paste("The third quartile of dataset one is",dataset_one_q3)), error=function(e) {print("You haven't defined dataset_one_q3")})
       
tryCatch(print(paste("The first quartile of dataset two is",dataset_two_q1)), error=function(e) {print("You haven't defined dataset_two_q1")})

tryCatch(print(paste("The third quartile of dataset two is",dataset_two_q3)), error=function(e) {print("You haven't defined dataset_two_q3")})
```

Codecademy Quartiles Q1 and Q3 3/6

#Sample Solution
---
title: "Quartiles"
output: html_notebook
---

```{r}
dataset_one <- c(50, 10, 4, -3, 4, -20, 2)
# sorted dataset_one: [-20, -3, 2, 4, 4, 10, 50]

dataset_two <- c(24, 20, 1, 45, -15, 40)
# sorted dataset_two: c(-15, 1, 20, 24, 40, 45)

dataset_one_q2 <- 4
dataset_two_q2 <- 22

# define the first and third quartile of both datasets here:
dataset_one_q1 <- -3
dataset_one_q3 <- 10
dataset_two_q1 <- 1
dataset_two_q3 <- 40
```

```{r}
# ignore the code below here:

tryCatch(print(paste("The first quartile of dataset one is",dataset_one_q1)), error=function(e) {print("You haven't defined dataset_one_q1")})

tryCatch(print(paste("The third quartile of dataset one is",dataset_one_q3)), error=function(e) {print("You haven't defined dataset_one_q3")})
       
tryCatch(print(paste("The first quartile of dataset two is",dataset_two_q1)), error=function(e) {print("You haven't defined dataset_two_q1")})

tryCatch(print(paste("The third quartile of dataset two is",dataset_two_q3)), error=function(e) {print("You haven't defined dataset_two_q3")})
```

This is an example of scrolling text using Javascript.

Popular Posts