Create your own version of the t-test. Please upload here your file. It can be an r-script of an rmd (markdown) file. Make sure that it works properly. Run your test on the following data, with null hypotheis mu=4, alternative mu>4 set.seed(1) x<-rexp(30,1/5) # random sample from exponential set.seed() is not working properly for me, so all of you may have different answers.
Create your own version of the t-test.
Please upload here your file. It can be an r-script of an rmd (markdown) file.
Make sure that it works properly.
Run your test on the following data, with null hypotheis mu=4, alternative mu>4
set.seed(1)
x<-rexp(30,1/5) # random sample from exponential
set.seed() is not working properly for me, so all of you may have different answers.
Here is an R-script that implements a one-sample t-test function:
CODE in R-Script:
# One-sample t-test function
one_sample_t_test <- function(x, mu, alternative = "two.sided", alpha = 0.05) {
n <- length(x)
xbar <- mean(x)
s <- sd(x)
se <- s / sqrt(n)
df <- n - 1
tstat <- (xbar - mu) / se
if (alternative == "less") {
pval <- pt(tstat, df)
if (pval < alpha) {
cat("Reject null hypothesis: mu >= ", mu, "\n")
} else {
cat("Fail to reject null hypothesis: mu >= ", mu, "\n")
}
} else if (alternative == "greater") {
pval <- pt(tstat, df, lower.tail = FALSE)
if (pval < alpha) {
cat("Reject null hypothesis: mu <= ", mu, "\n")
} else {
cat("Fail to reject null hypothesis: mu <= ", mu, "\n")
}
} else {
pval <- pt(abs(tstat), df, lower.tail = FALSE) * 2
if (pval < alpha) {
cat("Reject null hypothesis: mu = ", mu, "\n")
} else {
cat("Fail to reject null hypothesis: mu = ", mu, "\n")
}
}
cat("t-statistic:", tstat, "\n")
cat("p-value:", pval, "\n")
}
# Example usage
set.seed(1)
x <- rexp(30, 1/5)
one_sample_t_test(x, 4, alternative = "greater")
Step by step
Solved in 3 steps