This is chimpanzee.csv actor partner prosocial selfish prosocialcolor selfishcolor A I…
- User
This is chimpanzee.csv actor partner prosocial selfish prosocial_color selfish_color A I 22 8 green purple A G 16 14 yellow black A H 22 8 blue red A none 16 14 pink gray B H 18 12 black yellow B C 23 7 purple green B D 19 11 blue red B none 16 14 gray pink C B 18 12 yellow black C F 19 11 red blue C E 20 10 green purple C none 17 13 gray pink D C 19 11 black yellow D B 15 15 green purple D I 16 14 blue red D none 8 22 pink gray E C 14 16 purple green E I 14 16 blue red E J 20 10 black yellow E none 12 18 pink gray F I 18 12 green purple F A 18 12 red blue F D 11 19 black yellow F none 14 16 gray pink G J 16 14 red blue G F 15 15 black yellow G A 6 4 purple green
I am writing a rmd file that you have to fill in I will be writing in next prompt
- Assistant
Sure, I'd be happy to help! Please provide the details of your RMD file, and I will assist you in filling it in.
- User
title: "Homework 6" author: "ARNESH TARACHANDANI" output: html_document
knitr::opts_chunk$set(echo = TRUE, message=FALSE, fig.height = 3) library(tidyverse) source("../../scripts/ggprob.R") library(dplyr) library(ggplot2)1
For this question,
- Read in the same
chimpanzee.csvdata file we used for lecture/discussion. - Make an assumption that there are universal and parameters representing the probability ANY chimpanzee would make a prosocial choice in a single trial with or without a partner.
- Assume that all trials are independent.
Now, suppose we are interested in estimating the difference in sample proportions rather than just the individual population proportions. Confidence intervals can be defined for this purpose.
1A
Using the Wald estimates, construct a 90% confidence interval for the difference in the population proportions . (Note: you this proportion is treater as universal for all chimps, summarize your data across ALL chimps to get these estimates.)
# insert code here chimpanzees <- read.csv("../../data/chimpanzee.csv")1B
Interpret the estimated confidence interval in context.
2-4
For the next several questions, we will be using the Boston Marathon data set again. The following code chunk reads in the TIM.txt data file (you may need to adjust the path to the data), and makes the following modifications to the data frame.
The following variables are renamed:
bib=BibNumand changed to a character typeyear=Yearage=Agegender=Gender1F2M, change 1 to F and 2 to M
These variables are removed:
StartHr,StartMin,HalfMar,Age2014Also, the data set is filtered to only include the years 2011 and 2012 (i.e., observations from 2013 are removed).
tim = read_table("../../data/TIM.txt", guess_max = 20000) %>% rename(bib = BibNum, year = Year, age = Age, gender = Gender1F2M) %>% mutate(bib = as.character(bib), gender = case_when( gender == 1 ~ "F", gender == 2 ~ "M" )) %>% select(-c(StartHr,StartMin,HalfMar, "Age2014")) %>% filter(year != 2013)2
- Create a new variable called
finalthat gives the total time each runner took to run the marathon. This will involve summing the spit times; the split times are provided in 5 KM intervals starting withK0-5throughK35-40, and the final interval,K40-Fin, which is 2.2 KM. - Add a variable
speedthat has two possibles values,"Fast" or "Slow", depending on if the runner finished the marathon in under 3.5 hours or greater than or equal to 3.5 hours, respectively. - Print out a table that lists the total number of runners by gender and speed. There should be one row for Female and Fast, Female and Slow, Male and Fast, and Male and Slow.
# insert code here ``` ### 3 - Display a plot that has a point of `size=2` at the sample mean of the final time for each of the four combinations of `gender` and `speed`. - Facet the graphic into two plots based on the category of `speed` ("Fast" or "Slow"). - Add a blue (vertical) line segment to visualize a ***90% confidence interval*** for the mean final time for each gender and speed category. The lines should extend vertically from the lower bound to the upper bound of the confidence interval, and the point estimate from the previous step will be in the middle of the interval. - Label axes appropriately and add an informative title to the plot. Hint: Your final plot should have four vertical segments for the confidence intervals, a point in the center of each vertical line, and a horizontal dashed line. ``````{r} # insert code here ``` ### 4 - Consider only the runners with `speed` = "Slow". - Assume that the final time for each runner is independent. - Test the hypothesis that $\mu_F = \mu_M$ versus the two-sided alternative, where $\mu_F$ is the average final time for ("Slow") Female runners and $\mu_M$ is the average final time for ("Slow") Male runners. #### 4A What is the value of the observed test statistic? ``````{r} # insert code here ``` #### 4B What is the distribution of the test statistic assuming the null hypothesis is true? State the name of the distribution and the values of its parameter(s). ``````{r} # insert code here ``` #### 4C What is your computed p-value? ``````{r} # insert code here ``` #### 4D Interpret the result of the hypothesis test in context. ``````{r} # insert code here ``` - Read in the same
- Assistant
Let's fill in the R Markdown as per your requirements.
--- title: "Homework 6" author: "ARNESH TARACHANDANI" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, message=FALSE, fig.height = 3) library(tidyverse) source("../../scripts/ggprob.R") library(dplyr) library(ggplot2)1
For this question,
- Read in the same
chimpanzee.csvdata file we used for lecture/discussion. - Make an assumption that there are universal and parameters representing the probability ANY chimpanzee would make a prosocial choice in a single trial with or without a partner.
- Assume that all trials are independent.
Now, suppose we are interested in estimating the difference in sample proportions rather than just the individual population proportions. Confidence intervals can be defined for this purpose.
1A
Using the Wald estimates, construct a 90% confidence interval for the difference in the population proportions . (Note: you this proportion is treater as universal for all chimps, summarize your data across ALL chimps to get these estimates.)
# Read in the chimpanzee data chimpanzees <- read.csv("../../data/chimpanzee.csv") # Summarize the data across all chimps total_trials_partner <- sum(chimpanzees$prosocial) total_prosocial_partner <- sum(chimpanzees$prosocial) total_trials_no_partner <- sum(chimpanzees$selfish) total_prosocial_no_partner <- sum(chimpanzees$prosocial) # Proportions p_partner <- total_prosocial_partner / total_trials_partner p_no_partner <- total_prosocial_no_partner / total_trials_no_partner # Difference in proportions p_diff <- p_partner - p_no_partner # Standard error se_diff <- sqrt((p_partner * (1 - p_partner) / total_trials_partner) + (p_no_partner * (1 - p_no_partner) / total_trials_no_partner)) # Confidence Interval for 90% z <- qnorm(0.95) ci_lower <- p_diff - z * se_diff ci_upper <- p_diff + z * se_diff ci_lower ci_upper1B
Interpret the estimated confidence interval in context.
The 90% confidence interval for the difference in population proportions is approximately (`r ci_lower`, `r ci_upper`). This implies that, with 90% confidence, the true difference in the probability of making a prosocial choice between trials with a partner and trials without a partner lies within this interval.
2-4
For the next several questions, we will be using the Boston Marathon data set again. The following code chunk reads in the TIM.txt data file (you may need to adjust the path to the data), and makes the following modifications to the data frame.
The following variables are renamed:
bib=BibNumand changed to a character typeyear=Yearage=Agegender=Gender1F2M, change 1 to F and 2 to M
These variables are removed:
StartHr,StartMin,HalfMar,Age2014Also, the data set is filtered to only include the years 2011 and 2012 (i.e., observations from 2013 are removed).
tim = read_table("../../data/TIM.txt", guess_max = 20000) %>% rename(bib = BibNum, year = Year, age = Age, gender = Gender1F2M) %>% mutate(bib = as.character(bib), gender = case_when( gender == 1 ~ "F", gender == 2 ~ "M" )) %>% select(-c(StartHr,StartMin,HalfMar, "Age2014")) %>% filter(year != 2013)2
- Create a new variable called
finalthat gives the total time each runner took to run the marathon. This will involve summing the spit times; the split times are provided in 5 KM intervals starting withK0-5throughK35-40, and the final interval,K40-Fin, which is 2.2 KM. - Add a variable
speedthat has two possibles values,"Fast" or "Slow", depending on if the runner finished the marathon in under 3.5 hours or greater than or equal to 3.5 hours, respectively. - Print out a table that lists the total number of runners by gender and speed. There should be one row for Female and Fast, Female and Slow, Male and Fast, and Male and Slow.
# Summing split times to get the final time tim <- tim %>% mutate(final = rowSums(select(., starts_with("K")))) # Adding the speed variable tim <- tim %>% mutate(speed = ifelse(final < 3.5 * 60 * 60, "Fast", "Slow")) # Table of total number of runners by gender and speed table <- tim %>% group_by(gender, speed) %>% summarise(count = n()) table3
- Display a plot that has a point of
size=2at the sample mean of the final time for each of the four combinations ofgenderandspeed. - Facet the graphic into two plots based on the category of
speed("Fast" or "Slow"). - Add a blue (vertical) line segment to visualize a 90% confidence interval for the mean final time for each gender and speed category. The lines should extend vertically from the lower bound to the upper bound of the confidence interval, and the point estimate from the previous step will be in the middle of the interval.
- Label axes appropriately and add an informative title to the plot.
Hint: Your final plot should have four vertical segments for the confidence intervals, a point in the center of each vertical line, and a horizontal dashed line.
# Calculate the mean and standard error for each combination of gender and speed summary_stats <- tim %>% group_by(gender, speed) %>% summarise( mean_final = mean(final), se_final = sd(final) / sqrt(n()) ) # Calculate confidence interval bounds summary_stats <- summary_stats %>% mutate( ci_lower = mean_final - qnorm(0.95) * se_final, ci_upper = mean_final + qnorm(0.95) * se_final ) # Plotting ggplot(summary_stats, aes(x = gender, y = mean_final, color = gender)) + geom_point(size = 2) + geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2) + facet_wrap(~ speed) + labs(title = "Mean Final Time with 90% Confidence Interval", x = "Gender", y = "Mean Final Time (seconds)") + theme_minimal()4
- Consider only the runners with
speed= "Slow". - Assume that the final time for each runner is independent.
- Test the hypothesis that versus the two-sided alternative, where is the average final time for ("Slow") Female runners and is the average final time for ("Slow") Male runners.
4A
What is the value of the observed test statistic?
# Filter the data for slow runners slow_runners <- tim %>% filter(speed == "Slow") # Calculate the means and standard deviations mu_F <- mean(slow_runners$final[slow_runners$gender == "F"]) mu_M <- mean(slow_runners$final[slow_runners$gender == "M"]) sd_F <- sd(slow_runners$final[slow_runners$gender == "F"]) sd_M <- sd(slow_runners$final[slow_runners$gender == "M"]) n_F <- nrow(slow_runners[slow_runners$gender == "F",]) n_M <- nrow(slow_runners[slow_runners$gender == "M",]) # Calculate the test statistic t_stat <- (mu_F - mu_M) / sqrt((sd_F^2 / n_F) + (sd_M^2 / n_M)) t_stat4B
What is the distribution of the test statistic assuming the null hypothesis is true? State the name of the distribution and the values of its parameter(s).
# Degree of freedom using Welch-Satterthwaite equation df <- ((sd_F^2 / n_F + sd_M^2 / n_M)^2) / ((sd_F^2 / n_F)^2 / (n_F - 1) + (sd_M^2 / n_M)^2 / (n_M - 1)) dfThe test statistic follows a t-distribution with approximately `r round(df, 2)` degrees of freedom.
4C
What is your computed p-value?
# Compute the p-value p_value <- 2 * pt(-abs(t_stat), df) p_value4D
Interpret the result of the hypothesis test in context.
# Interpretation of the p-value if (p_value < 0.05) { interpretation <- "We reject the null hypothesis. There is significant evidence to suggest that there is a difference in average final time between slow female and slow male runners." } else { interpretation <- "We fail to reject the null hypothesis. There is not enough evidence to suggest that there is a difference in average final time between slow female and slow male runners." } interpretationMake sure to replace the `"../../data/TIM.txt"` and `"../../data/chimpanzee.csv"` with the correct file path to your data files. - Read in the same