1

I'm trying to perform a one sample t test with given mean and sd.

Sample mean is 100.5 population mean is 100 population standard deviation is 2.19 and sample size is 50.

although it is relatively simple to perfrom a t-test with a datasheet, I don't know how to perform a t test with given data.

What could be the easiest way to write this code?

I would like to get my t test value, df value and my p-value just like what the code t.test() gives you.

I saw another post similar to this. but it didn't have any solutions.

I couldn't find any explanation for performing one sample t test with given mean and sd.

  • sample_mean <- 100.5 # the mean of your sample sample_sd <- 2.19 # the standard deviation of your sample sample_data <- rnorm(n=50, mean=sample_mean, sd=sample_sd) t.test(sample_data, mu=100) #specify the population mean #When I typed this code, it gave me this :t = 0.31576, df = 49, p-value = 0.7535 but I know, that shouldn't be my t-value. – Erkaya Mustafa Feb 14 '23 at 00:26

1 Answers1

0

Since the parameters of the population is known (mu=100, sigma=2.19) and your sample size is greater than 30 (n=50), it is possible to perform either z-test or t-test. However, the base R doesn't have any function to do z-test. There is a z.test() function in the package BSDA (Arnholt and Evans, 2017):

library(BSDA)
z.test (
      x = sample_data # a vector of your sample values
     ,y= NULL # since you are performing one-sample test
     ,alternative = "two.sided"
     ,mu = 100 # specified in the null hypothesis
     ,sigma.x = 2.19
)

Similarly the t.test can be performed using base R function t.test():

t.test(sample_data
       , mu=100
       , alternative = "two-sided"
 )

The question is that which test we might consider to interpret our result?

  • The z-test is only an approximate test (i.e. the data will only be approximately Normal), unlike the t-test, so if there is a choice between a z-test and a t-test, it is recommended to choose t-test (The R book by Michael J Crawley and colleagues).
  • Also choosing between one-sided or two-sided is important,If the difference between μ and μ0 is not known, a two-sided test should be used. Otherwise, a one-sided test is used.

Hope this could helps.

S-SHAAF
  • 1,863
  • 2
  • 5
  • 14
  • Thanks a lot! After a couple of tries, I understood why it was giving me different t values as well. R was trying to generate its own values since I wasn't giving it enough data. – Erkaya Mustafa Feb 14 '23 at 02:37