2
$\begingroup$

I would like to calculate Student's t test of two normal distribution data. However, I do not have the data at all, but only mean and standard deviation of each population. So, how can I simulate those data in R and calculate the t.test among both?

These are the values (mean and SD): Population 1: 6,62 +- 0.52 years Population 2: 6.31 +- 0.49 years

Thanks in advance Mario

$\endgroup$
2
$\begingroup$

You can do something very like what you originally asked.

The usual t-tests - including the Welch-Satterthwaite version - depend only on the summary statistics; mean, standard deviation and sample size (even the paired test depends depends on those summary statistics -- for the pair-differences).

So you can simulate data -- the distribution doesn't matter -- as long as it has the right mean, standard deviation and sample size. (Your example doesn't give the same sizes!)

I'm going to illustrate with two examples where I'll assume n1=10 and n2=12 but using your mean and variance:

# example: 
# step 0: set up summary statistics
m1=6.62; s1=0.52; n1=10
m2=6.31; s2=0.49; n2=12

# method 1: normal samples, rescaled to match original:
z1 = rnorm(n1)
x1 = scale(z1)*s1+m1
z2 = rnorm(n2)
x2 = scale(z2)*s2+m2
t.test(x1,x2) # Welch-Satterthwaite
t.test(x1,x2,var.equal=TRUE) # equal variance

# distribution 2: uniform samples, rescaled to match original:
z1 = runif(n1)
x1 = scale(z1)*s1+m1
z2 = runif(n2)
x2 = scale(z2)*s2+m2
t.test(x1,x2) # Welch-Satterthwaite
t.test(x1,x2,var.equal=TRUE) # equal variance

Summarizing the results from that:

Distrib.    t-test  
Sampled     type       t-stat   df        p

Normal      Welch      1.4292  18.817   0.1693
Uniform     Welch      1.4292  18.817   0.1693

Normal      Eq.Var     1.4373   20      0.1661
Uniform     Eq.Var     1.4373   20      0.1661

So as you suggest in your question, you can simulate; if your simulated values are in z1, then in R, x1 = scale(z1)*s1+m1 (where m1 and s1 are the required sample mean and variance) gives you a sample with the summary stats you need. (Note that scale by default standardizes for the sample mean and sd).

Similar tricks would work in most packages.

$\endgroup$
7
$\begingroup$

For the t.test function, you need the actual data.

The following function, T.test, allows you to compute a t-test based on means, standard deviations, and numbers of observations:

T.test <- function(n, mean, sd) {
  s <- sum((n - 1) * sd^2) / (sum(n) - 2) # weighted variance
  t <- sqrt(prod(n) / sum(n)) * (diff(mean) / sqrt(s)) # t statistic
  df <- sum(n) - 2  # degrees of freedom
  p <- (1 - pt(abs(t), df)) * 2 # p value
  c(t = t, p = p)
}

If you do not know n, you cannot compute the t-test. For the following example, I assume that $n_1 = 10$ and $n_2 = 12$

dat <- data.frame(mean = c(6.62, 6.31), sd = c(.52, .49), n = c(10, 12))
#   mean   sd  n
# 1 6.62 0.52 10
# 2 6.31 0.49 12

T.test(dat$n, dat$mean, dat$sd)
#          t          p 
# -1.4373111  0.1660915 

The function returns the $t$ statistic and the corresponding $p$-value.

$\endgroup$
  • $\begingroup$ Hi Sven, Thanks for your answer. That is what I really want. However, how can I install T.test function? When I do a simply t.test, there is no problem, but when doing a T.test, my software (Rstudio) says that this function is not found. Thanks $\endgroup$ – antecessor Mar 11 '14 at 16:22
  • $\begingroup$ You will need to copy paste Sven's nice function to your editor and execute it in R first. Then you can use it. $\endgroup$ – Michael M Mar 11 '14 at 16:29
  • $\begingroup$ Yes! Perfect! Now I can use it without any problem. I thought it was going to be more complicated. But another time, the simpliest feature is the correct one! Thanks! $\endgroup$ – antecessor Mar 11 '14 at 16:34
  • $\begingroup$ How can I change the probability, from p=.05 to p=.01? $\endgroup$ – antecessor Mar 11 '14 at 17:04
  • $\begingroup$ @antecessor Just compare the returned p-value with your criterion. If the p-value is higher than your criterion, the two samples do not differ significantly. $\endgroup$ – Sven Hohenstein Mar 11 '14 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.