library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(dplyr)
library(Sleuth3)
Sleuth3Manual()

Chapter 2

Computational Exercise 15

Downtown Cairo

n = 126
mean = 29.2
SD = 7.5

Cairo Suburb (control)

n = 50
mean = 18.2
SD = 5.8

Pooled SD

s.p <- sqrt(((126-1)*(7.5^2) + (50-1)*(5.8^2))/(126+50-1))

The pooled SD of 7.04 is high, indicating that there is a large variance between the population means

Standard Error

ydiff.se <- s.p * sqrt(1/126 + 1/50)

t-statistic

qt(0.975, df=174)
## [1] 1.973691

The t-statistic is high which will result to a small p-value

p-value

1-pt(2.54, 174)
## [1] 0.00598014

p < 0.001, this indicates that there is a convincing evidence of a difference between the population means, therefore we reject the null hypothesis that there is no difference between the population means.

Data Problem 22

data(ex0222)

female group

females <- subset(ex0222, Gender == "female")

male group

males <- subset(ex0222, Gender == "male")

Plotting a boxplot

ggplot(ex0222, aes(x = Gender, y = AFQT)) +
  geom_boxplot() +
  theme_bw()

Looking at the box plot, the median of AFQT scores for the male group is higher than that for the female group, however the difference is not significant. The length of the whiskers for both gender groups seems to not be long suggesting less variability within the groups. There are no outliers in both groups, indicating less variability of the data.

t.test analysis

t.test(AFQT ~ Gender,data = ex0222)
## 
##  Welch Two Sample t-test
## 
## data:  AFQT by Gender
## t = -1.8701, df = 2578.1, p-value = 0.06158
## alternative hypothesis: true difference in means between group female and group male is not equal to 0
## 95 percent confidence interval:
##  -4.179916  0.099000
## sample estimates:
## mean in group female   mean in group male 
##             53.40579             55.44625

A Welch Two Sample t-test was used to generate an analysis of the AFQT scores by gender. There is a 2.04046 difference between the AQFT scores mean in the female group and the male group. The t-stat is small, which results in the p-value being high (p = 0.06). Thus p > 0.05 indicates that the evidence that there is a difference between the AFQT scores of males and females is suggestive but inconclusive, thus the evidence is not convincing.

Chapter 3

Computational Exercise 21

load in data

data("ex0321")

transform data into long format

long_data <- ex0321 %>% pivot_longer(cols = c("Lifelength", "Expected"), names_to = "longdata", values_to = "longvalues")

log transform the long values

log_data <- long_data %>% mutate(years.log = log(longvalues))

question (a)

long_data %>% filter(Censored == 0) %>% select(longdata, longvalues)
## # A tibble: 390 × 2
##    longdata   longvalues
##    <chr>           <int>
##  1 Lifelength         63
##  2 Expected           70
##  3 Lifelength         69
##  4 Expected           71
##  5 Lifelength         58
##  6 Expected           71
##  7 Lifelength         70
##  8 Expected           70
##  9 Lifelength         68
## 10 Expected           69
## # ℹ 380 more rows
new_data <- long_data %>% filter(Censored == 0) %>% select(longdata, longvalues)
t.test(longvalues ~ longdata, data = new_data)
## 
##  Welch Two Sample t-test
## 
## data:  longvalues by longdata
## t = 0.98786, df = 215.09, p-value = 0.3243
## alternative hypothesis: true difference in means between group Expected and group Lifelength is not equal to 0
## 95 percent confidence interval:
##  -0.5359136  1.6128366
## sample estimates:
##   mean in group Expected mean in group Lifelength 
##                 69.01538                 68.47692

The mean observed life lengths is lower than the mean expected life length, however the difference is not significant. The t-statistic is small which results in the p-value being large. P > 0.05 indicates that the evidence that the umpires had smaller observed life lengths than expected is suggestive but inconclusive, thus the evidence is not convincing.

question (b)

If a portion of the population is left out of the study, the sample could no longer represent the population it is meant to generalize to. This may cause selection bias where the life lengths of the individuals are consistently included differ from the life lengths of those individuals who are not included. Additionally, this may lead to the variability being underestimated, leading to less precise estimates and wider confidence intervals.

question (c)

Ignoring those 32 umpires in the data set who had not yet died at the time of the study can lead to biased estimates of survival probabilities as they are not being observed. The variability in the survival times may be underestimated, leading to less precise estimates and narrow confidence intervals.

Data Problem 30

data("ex0330")

Summary stats

summary(ex0330)
##     Subject           Educ         Income2005    
##  Min.   :    2   Min.   :12.00   Min.   :   200  
##  1st Qu.: 1590   1st Qu.:12.00   1st Qu.: 21954  
##  Median : 3038   Median :12.00   Median : 35000  
##  Mean   : 3503   Mean   :13.14   Mean   : 46298  
##  3rd Qu.: 4628   3rd Qu.:16.00   3rd Qu.: 57375  
##  Max.   :12139   Max.   :16.00   Max.   :519340

Plot a boxplot

ggplot(ex0330, aes(x = as.factor(Educ), y = Income2005)) +
  geom_boxplot() +
  xlab("Education Level") +
  ylab("Income2005") +
  theme_bw()

The Income of the 16 education level has more variability of data compared to the income of the 12 education level.

t.test

t.test(Income2005 ~ as.factor(Educ), data = ex0330)
## 
##  Welch Two Sample t-test
## 
## data:  Income2005 by as.factor(Educ)
## t = -9.9827, df = 473.85, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group 12 and group 16 is not equal to 0
## 95 percent confidence interval:
##  -39653.77 -26610.39
## sample estimates:
## mean in group 12 mean in group 16 
##         36864.90         69996.97

The population distribution of incomes for those with 16 years of education exceeds the distribution for those with 12 years of education with $33131.77. The confidence interval is wide, the t-statistic is high which indicates that the p-value is small p < 0.001. The 95% confidence interval is wide.

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.