Complete all of the problems below and enter your code within the supplied code block. If code is already included in the code block, this is for the setup of the problem. If the problem does not have a code component, write your response as a comment in the R code block. Note: This assignment is meant to be done in order.
Create a function named calculate_circle_area that takes the radius of a circle as an argument and returns the area of the circle. Call the function with a radius of 3.
# Create a function to calculate the area of a circle
calculate_circle_area <- function(radius) {
area <- pi * radius^2
return(area)
}
# Call the function with radius = 3
calculate_circle_area(3)
## [1] 28.27433
#Your code here
Create a function named calculate_factorial that takes a non-negative integer n as an argument and returns the factorial of n. The factorial of 0 is defined as 1. Call the function with n equal to 5.
# Create a function to calculate factorial
calculate_factorial <- function(n) {
if (n == 0) {
return(1)
} else {
result <- 1
for (i in 1:n) {
result <- result * i
}
return(result)
}
}
# Call the function with n = 5
calculate_factorial(5)
## [1] 120
Write an if else statement that checks if a given number is positive or negative. If the number is positive, print “Number is positive,” if the number is negative print “Number is negative.” Otherwise, print “Number is zero.”
# Check if a number is positive, negative, or zero
num <- 0 # You can change this value to test
if (num > 0) {
print("Number is positive")
} else if (num < 0) {
print("Number is negative")
} else {
print("Number is zero")
}
## [1] "Number is zero"
Write a for loop to calculate the sum of all even numbers from 1 to 20.
# Calculate the sum of all even numbers from 1 to 20
sum_even <- 0
for (i in 1:20) {
if (i %% 2 == 0) {
sum_even <- sum_even + i
}
}
sum_even
## [1] 110
A fitness company claims that their member’s running time for a 5 km race is 20 minutes on average. You collect data from a sample of 50 participants (running times in minutes).
Test, at significance level \(\alpha = 0.01\): \[ H_0:\ \mu = 20 \quad \text{vs.} \quad H_1:\mu \neq 20 \] Assume the data are approximately normal.
Use the following data:
# Running time hypothesis test
running_times <- c(
16, 20, 17, 19, 18, 15, 21, 22, 19, 23,
17, 16, 18, 19, 20, 24, 16, 21, 22, 18,
17, 19, 18, 20, 23, 15, 19, 18, 20, 21,
17, 22, 19, 20, 18, 16, 23, 19, 21, 17,
20, 18, 19, 15, 22, 21, 17, 18, 19, 20
)
# Perform a one-sample t-test
t.test(running_times, mu = 20, alternative = "two.sided", conf.level = 0.99)
##
## One Sample t-test
##
## data: running_times
## t = -2.9938, df = 49, p-value = 0.004309
## alternative hypothesis: true mean is not equal to 20
## 99 percent confidence interval:
## 18.18064 19.89936
## sample estimates:
## mean of x
## 19.04
A test-prep course claims to increase students’ exam scores. You collect pre- and post-course scores from the same 30 students. Test
\[ H_0:\ \mu_{\text{post}-\text{pre}} \le 0 \quad \text{vs.} \quad H_1:\ \mu_{\text{post}-\text{pre}} > 0 \]
at significance level \(\alpha = 0.05\). Also report a 95% confidence interval for the mean difference.
pre_scores <- c(80,81,69,76,84,83,80,77,83,79,
74,84,72,77,72,71,76,85,72,77,
71,70,78,83,85,71,79,81,78,74)
post_scores <- c(89,90,75,78,86,88,86,80,89,87,
78,90,80,85,78,79,83,93,77,80,
79,74,83,89,92,79,83,86,83,81)
# Paired t-test
t.test(post_scores, pre_scores, alternative = "greater", paired = TRUE, conf.level = 0.95)
##
## Paired t-test
##
## data: post_scores and pre_scores
## t = 16.258, df = 29, p-value < 2.2e-16
## alternative hypothesis: true mean difference is greater than 0
## 95 percent confidence interval:
## 5.313257 Inf
## sample estimates:
## mean difference
## 5.933333
A company advertises that 60% of customers prefer their product over competitors. You take a random sample of 120 customers, and find that 65 of them prefer the product.
Conduct a two-sided hypothesis test at significance level \(\alpha = 0.1\):
\[ H_0:\ p = 0.60 \quad \text{vs.} \quad H_1:\ p \neq 0.60 \]
# Hypothesis test for population proportion
# H0: p = 0.60 vs H1: p ≠ 0.60
n <- 120
x <- 65
prop.test(x, n, p = 0.6, alternative = "two.sided", conf.level = 0.90, correct = FALSE)
##
## 1-sample proportions test without continuity correction
##
## data: x out of n, null probability 0.6
## X-squared = 1.7014, df = 1, p-value = 0.1921
## alternative hypothesis: true p is not equal to 0.6
## 90 percent confidence interval:
## 0.4667558 0.6147401
## sample estimates:
## p
## 0.5416667