Below are the solutions to these exercises on inferential statistics.

#################### # # # Exercise 1 # # # #################### n <- 25 m <- 29 std <- 4 x <- rnorm(n, mean = m, sd = std) t <- sqrt(n)*(mean(x)-mean(data$mass))/sd(x); t

## [1] -3.992123

#################### # # # Exercise 2 # # # #################### p = 2*pt(-abs(t),n-1); p

## [1] 0.0005374991

#################### # # # Exercise 3 # # # #################### qt(0.025, n - 1); qt(0.975, n - 1)

## [1] -2.063899

## [1] 2.063899

# We reject the null hypothesis since the t value doesn't belong to the confidence interval #################### # # # Exercise 4 # # # #################### t.test(x,mu=mean(data$mass))

## ## One Sample t-test ## ## data: x ## t = -3.9921, df = 24, p-value = 0.0005375 ## alternative hypothesis: true mean is not equal to 31.99258 ## 95 percent confidence interval: ## 27.14068 30.44775 ## sample estimates: ## mean of x ## 28.79421

#################### # # # Exercise 5 # # # #################### t.test(x, mu=mean(data$mass), alternative = c("less"))

## ## One Sample t-test ## ## data: x ## t = -3.9921, df = 24, p-value = 0.0002687 ## alternative hypothesis: true mean is less than 31.99258 ## 95 percent confidence interval: ## -Inf 30.16492 ## sample estimates: ## mean of x ## 28.79421

#################### # # # Exercise 6 # # # #################### n <- 27 m <- 31 std <- 5 y <- rnorm(n, mean = m, sd = std) t.test(x,y, var.equal=FALSE)

## ## Welch Two Sample t-test ## ## data: x and y ## t = -1.0558, df = 47.52, p-value = 0.2964 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## -4.062233 1.265441 ## sample estimates: ## mean of x mean of y ## 28.79421 30.19261

#################### # # # Exercise 7 # # # #################### t.test(x,y, var.equal=FALSE, paired=FALSE, alternative = c("less"))

## ## Welch Two Sample t-test ## ## data: x and y ## t = -1.0558, df = 47.52, p-value = 0.1482 ## alternative hypothesis: true difference in means is less than 0 ## 95 percent confidence interval: ## -Inf 0.823576 ## sample estimates: ## mean of x mean of y ## 28.79421 30.19261

#################### # # # Exercise 8 # # # #################### n <- 27 m <- 31 std <- 4 z <- rnorm(n, mean = m, sd = std) t.test(x, z, var.equal=TRUE, paired=FALSE)

## ## Two Sample t-test ## ## data: x and z ## t = -1.9891, df = 50, p-value = 0.05218 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## -4.12925164 0.02012504 ## sample estimates: ## mean of x mean of y ## 28.79421 30.84878

#################### # # # Exercise 9 # # # #################### t.test(x, z, var.equal=TRUE, paired=FALSE, alternative = c("less"))

## ## Two Sample t-test ## ## data: x and z ## t = -1.9891, df = 50, p-value = 0.02609 ## alternative hypothesis: true difference in means is less than 0 ## 95 percent confidence interval: ## -Inf -0.3234813 ## sample estimates: ## mean of x mean of y ## 28.79421 30.84878

#################### # # # Exercise 10 # # # #################### n <- 27 m_1 <- 29 std_1 <- 4 t_1 <- rnorm(n, mean = m_1, sd = std_1) n <- 27 m_2 <- 28 std_2 <- 4 t_2 <- rnorm(n, mean = m_2, sd = std_2) t.test(t_1, t_2, var.equal=FALSE, paired=TRUE)

## ## Paired t-test ## ## data: t_1 and t_2 ## t = 0.15799, df = 26, p-value = 0.8757 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## -1.744633 2.035142 ## sample estimates: ## mean of the differences ## 0.1452542

**What's next:**

- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

Quan Nguyen says

Great tutorials. There is a small problem with exercise 8 which makes the variable z not initialized properly.

Vasileios Tsakalos says

Hello Quan !

Thanks for noticing it, I have updated the solutions properly. Your assistance is very much appreciated ! Thanks for the good words.

Cheers!