Create a function in R to perform a two-tailed one sample t-test. Assume that po
ID: 3220784 • Letter: C
Question
Create a function in R to perform a two-tailed one sample t-test. Assume that population SD is unknown. Find the p-value for H0: mu=100 vs H1: mu does not = 100.*** I am not supposed to use the ttest function. I'm supposed to create my own function to solve this problem.
The format may be similar to something like this:
fn.name = function(-,-,...) { test stat = [mean()-.......] p-value = ... print(p-value) } Create a function in R to perform a two-tailed one sample t-test. Assume that population SD is unknown. Find the p-value for H0: mu=100 vs H1: mu does not = 100.
*** I am not supposed to use the ttest function. I'm supposed to create my own function to solve this problem.
The format may be similar to something like this:
fn.name = function(-,-,...) { test stat = [mean()-.......] p-value = ... print(p-value) }
*** I am not supposed to use the ttest function. I'm supposed to create my own function to solve this problem.
The format may be similar to something like this:
fn.name = function(-,-,...) { test stat = [mean()-.......] p-value = ... print(p-value) }
Explanation / Answer
R code:
#x is a vector containing the data
#m0 is the hypothesized population mean
t.test1Sample <- function(x,m0){
m1 = mean(x)
s = sd(x)
n = length(x)
se = s / sqrt(n)
df = n - 1
t = (m1-m0)/se
p = 2*pt(-abs(t),df)
data <- c(m1-m0, se, t, p)
names(data) <- c("Difference of means", "Std Error", "t", "p-value")
return(data)
}
Trial output:
Please note: the entire sample as a vector is the input for this function. If required, this can be modified to provide the sample mean, sample standard deviation and the length of the sample.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.