Create a function in R to perform a two-tailed one sample t-test. Assume that po
ID: 3816224 • 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
# m1= a sample of data to which t.test is to be done.
# s1= std deviation of the data
# n1= size of the dataset
#m0: the null value for the difference in means to be checked. Default is 0.
# equal.variance: to check if equal variance is to be used. Default is FALSE.
t.test2 <- function(m1,s1,n1,m0=0,equal.variance=FALSE)
{
if( equal.variance==FALSE )
{
se <- sqrt (s1^2/n1)
df <- ( (s1^2/n1 )/( (s1^2/n1)^2/(n1-1)) )
} else
{
se <- sqrt( (1/n1) * ((n1-1)*s1^2/(n1-1) ))
df <- n1-1
}
t <- (m1-m0)/se
dat <- c(m1, se, t, 2*pt(-abs(t),df))
names(dat) <- c("mean", "Std Error", "t", "p-value")
return(dat)
}
# taking example seq as X1. when mean is to be 100
x1 = rnorm(100, mean=100, sd=1)
#applying the above function to get result
t.test2( mean(x1), sd(x1), 100)
# taking example seq as X1. when mean is not to be 100
x1 = rnorm(100, mean=50, sd=1)
#applying the above function to get result
t.test2( mean(x1), sd(x1), 100)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.