Math 143: Introduction to Biostatistics R Pruim Spring 2012
0.2 Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
Contents 10 More About Random Variables 1 10.1 The Mean of a Random Variable.................................... 1 10.2 The Variance of a Random Variable.................................. 3 10.3 Expected Value and Variance for Combinations............................ 5 11 Inference for One Mean 1 11.1 The Long Way.............................................. 1 11.2 Super Short Cuts............................................. 2 11.3 Confidence Intervals for Proportions.................................. 2 12 Comparing Two Means 1 12.1 Summary of Methods.......................................... 1 12.2 Paired t.................................................. 2 12.3 Two Sample T.............................................. 5 3
9.4 Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
More About Random Variables 10.1 10 More About Random Variables 10.1 The Mean of a Random Variable 10.1.1 A motivating example: GPA computation Let s begin with a motivating example. Suppose a student has taken 10 courses and received 5 A s, 4 B s, and 1 C. Using the traditional numerical scale where an A is worth 4, a B is worth 3, and a C is worth 2, what is this student s GPA (grade point average)? The first thing to notice is that 4+3+2 3 = 3 is not correct. We cannot simply add up the values and divide by the number of values. Clearly this student should have a GPA that is higher than 3.0, since there were more A s than C s. Consider now a correct way to do this calculation: GPA = 4 + 4 + 4 + 4 + 4 + 3 + 3 + 3 + 3 + 2 10 = 5 4 + 4 3 + 1 2 10 = 5 10 4 + 4 10 3 + 1 10 2 5 = 4 10 + 3 4 10 + 2 1 10 = 3.4. Our definition of the mean of a random variable follows the example above. Notice that we can think of the GPA as a sum of products: GPA = (grade)(probability of getting that grade). Such a sum is often called a weighted sum or weighted average of the grades (the probabilities are the weights). The expected value of a discrete random variable is a similar weighted average of its possible values. Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
10.2 More About Random Variables Let X be a discrete random variable with pmf f. The mean (also called expected value) of X is denoted as µ X or E(X) and is defined by µ X = E(X) = x x Pr(X = x). The sum is taken over all possible values of X. 10.1.2 Example: Daily 3 Lottery If you play the Daily 3 lottery game straight you pick a three-digit number and if it exactly matches the three-digit number randomly chosen by the lottery commission, you win $500. What is the expected value of lottery ticket? Let X = the value of the ticket. Then value of X 0 500 probability 999/1000 1/1000 because there is only one winning combination. So E(X) = 0 999 1000 + 500 1 1000 = 0.50 So the expected value of the ticket is 50c. The lottery commission charges $1 to play the game. This means the lottery commission averages a gain of 50c per play. (And those who play lose 50c on average per play.) 10.1.3 Another Example: Four Coins If we flip four fair coins and let X count the number of heads, what is E(X)? Recall that if we flip four fair coins and let X count the number of heads, then the distribution of X is described by the following table: value of X 0 1 2 3 4 probability 1 16 4 16 6 16 4 16 1 16 So the expected value is 0 1 16 + 1 4 16 + 2 6 16 + 3 4 16 + 4 1 16 = 2. On average we get 2 heads in 4 tosses. Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
More About Random Variables 10.3 10.2 The Variance of a Random Variable The variance of a random variable is computed very much like the mean. We just replace the values of the distribution with (value expected value) 2 In other words, the variance is the expected value of (X E(X)) 2. The standard deviation is the square root of the variance. 10.2.1 Example: GPA Returning to our GPA example value 4 3 2 probability.5.4.2 Recall that the expected value is 3.4. We can compute the variance as follows. variance = (4 3.4) 2 0.5 + (3 3.4) 2 0.4 + (2 3.4) 2 0.1 = 0.44 10.2.2 Example: Bin(2,.05) Let X Binom(2, 0.5). We can compute the expected value and the variance, recalling our probability table: value of X 0 1 2 probability 0.25 0.50 0.25 and E(X) = 0 0.25 + 1 0.5 + 2 0.25 = 0 + 0.5 + 0.5 = 1.0. Var(X) = (0 1) 2 0.25 + (1 1) 2 0.5 + (2 1) 2 0.25 = 0.25 + 0 + 0.25 = 0.5. 10.2.3 Example Find the mean and variance of the random variable Y described below: value of Y 1 2 3 4 probability 0.05 0.20 0.40 0.35 Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
10.4 More About Random Variables 10.2.4 Bernoulli Random Variable If X Binom(1, p), then X is called a Bernoulli random variable with probability p. For a Bernoulli random variable X, what are E(X) and Var(X)? We begin by building the probability table for this variable: value of X 0 1 probability 1 p p Now we do the calculations: and E(X) = 0 (1 p) + 1 p = p Var(X)(0 p) 2 (1 p) + (1 p) 2 p = p 2 (1 p) + (1 p) 2 p = p(1 p)(p + (1 p)) = p(1 p). We can calculate the variance directly from the definition: Var(X) = (0 p) 2 (1 p) + (1 p) 2 p = p 2 (1 p) + p(1 p) 2 = p(1 p) [(1 p) + p] = p(1 p). As a function of p, Var(X) is quadratic. Its graph is a parabola that opens downward, and since Var(X) = 0 when p = 0 and when p = 1, the largest variance occurs when p = 1 2. plotfun(p * (1 - p) ~ p, p.lim = c(0, 1)) p * (1 p) 0.25 0.20 0.15 0.10 0.05 0.00 0.2 0.4 0.6 0.8 p Any random variable that only takes on the values 0 and 1 is a Bernoulli random variable. Bernoulli random variables will play an important role in determining the variance of a general binomial random variable. Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
More About Random Variables 10.5 10.3 Expected Value and Variance for Combinations The following rules often make calculating means and expected values much simpler. Let X and Y be random variables and let a be a constant. Then 1. E(a + X) = a + E(X) 2. E(aX) = a E(X) 3. E(X + Y ) = E(X) + E(Y ) 4. E(X Y ) = E(X) + E(Y ) 5. Var(a + X) = Var(X) 6. Var(aX) = a 2 Var(X) 7. Var(X + Y ) = Var(X) + Var(Y ), provided X and Y are independent. (We ll call this the Pythagorean theorem for variance.) 10.3.1 Example Suppose X and Y are independent and satisfy X Y mean 50 60 standard deviation 3 4 Then E(X + Y ) = E(X) + E(Y ) = 50 + 60 = 110 Var(X + Y ) = Var(X) + Var(Y ) = 3 2 + 4 2 = 25 So the standard deviation of X is 5. 10.3.2 Binomial Random Varaibles We can now determine the mean and variance of any Binomial random variable. Let X Binom(n, p). Then X = X 1 + X 2 + + X n where each X i Binom(1, p). So and E(X) = E(X 1 ) + E(X 2 ) + E(X n ) = p + p + + p = np Var(X) = Var(X 1 ) + Var(X 2 ) + Var(X n ) = p(1 p) + p(1 p) + + p(1 p) = np(1 p) Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
10.6 More About Random Variables Normal Distributions in RStudio The two main functions we need for working with normal distributions are pnorm() and qnorm(). pnorm() works just like pbinom(): when X Norm(µ, σ). pbinom(x,mean=µ, sd=σ) = Pr(X x) # P( X <= 700); X ~ Norm(500, 100) pnorm(700, 500, 100) [1] 0.9772 # P( X >= 700); X ~ Norm(500, 100) 1 - pnorm(700, 500, 100) [1] 0.02275 qnorm() goes the other direction. You provide the quantile (percentile expressed as a decimal) and R gives you the value. # find 80th percentile in Norm(500, 100) qnorm(0.8, 500, 100) [1] 584.2 The xpnorm() function gives a bit more verbose output and also gives you a picture. xpnorm(700, 500, 100) If X ~ N(500,100), then P(X <= 700) = P(Z <= 2) = 0.9772 P(X > 700) = P(Z > 2) = 0.0228 density 0.005 0.004 0.003 0.002 (z=2) 0.9772 0.0228 0.001 200 400 600 800 [1] 0.9772 Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
More About Random Variables 10.7 Exercises 1 Consider the random variable X with probabilities given in the table below: x 0 1 2 3 P (X = x).1.2.3.4 a) What is the expected value of X? b) What is the variance of X? 2 Two fair dice (6-sided) are to be rolled. Let Y be the larger of the two values rolled. a) What is P (Y = 1)? b) What is P (Y = 6)? c) What is P (Y = 1 Y 6)? d) Compute E(Y ), the expected value of Y. 3 Two fair four-sided dice are to be rolled. Let Y be the larger of the two values rolled. a) What is P (Y = 1)? b) What is P (Y = 4)? c) What is P (Y = 1 Y 4)? d) Compute E(Y ), the expected value of Y. 4 Consider the game of roulette. An American roullette wheel has slots numbered 1 through 36 on it, half red and half black. In addition there are two green slots (numbered 0 and 00). That makes 38 slots altogether. A $1 wager on black returns the wager plus an additional $1 if a black number comes up, else the wager is lost. (So a player either wins $1 or loses $1.) a) What is the expected profit per roulette play for a casino? (This is, of course, also the expected loss per play for the players.) b) Suppose a casino estimates that it costs $50 per hour to run the roulette table. (They have to pay for heat and lights, for people to run the table, etc.) How much money must be bet per hour for the casino to break even? 5 Weird Willy offers you the following choice. You may have 1/3.5 dollars, or you may roll a fair die and he will give you 1/X dollars where X is the value of the roll. Which is the better deal? Compute E(1/X) to decide. 6 Suppose X and Y are independent random variables and E(X) = 24, Var(X) = 4, E(Y ) = 22, and Var(Y ) = 9. a) What is the standard deviation of X? b) What is the standard deviation of Y? c) What is E(2X)? d) What is Var(2X)? e) What is E(X + Y )? f) What is Var(X + Y )? g) What is E(X Y )? h) What is Var(X Y )? Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
10.8 More About Random Variables 7 Suppose X and Y are independent random variables satisfying X Y mean 100 90 standard deviation 6 8 a) What is E(2X)? b) What is Var(2X)? c) What is E(X + Y )? d) What is Var(X + Y )? e) What is E(X Y )? f) What is Var(X Y )? 8 Let X Binom(20,.8). a) What is the probability that X = 16? b) What is the probability that X 16? c) Compute E(X). d) Compute Var(X). Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
More About Random Variables 10.9 Solutions 1 # Expected value m <- 0 * 0.1 + 1 * 0.2 + 2 * 0.3 + 3 * 0.4; m [1] 2 # Variance (0-m)^2 * 0.1 + (1-m)^2 * 0.2 + (2-m)^2 * 0.3 + (3-m)^2 * 0.4 [1] 1 2 value of Y 1 2 3 4 5 6 probability 1/36 3/36 5/36 7/36 9/36 11/36 P (Y = 1 Y 6) = 1/36 25/36 = 1 25. # Expected value 1 * 1/36 + 2 * 3/36 + 3 * 5/36 + 4 * 7/36 + 5 * 9/36 + 7 * 11/36 [1] 4.778 3 value of Y 1 2 3 4 probability 1/16 3/16 5/16 7/16 P (Y = 1 Y 4) = 1/16 7/16 = 1 7. # Expected value m <- -1 * 20/38 + 1 * 18/38 m [1] -0.05263 # How many plays to cover $50? 50/abs(m) [1] 950 4 Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
10.10 More About Random Variables # Expected value m <- -1 * 20/38 + 1 * 18/38 m [1] -0.05263 # How many plays to cover $50? 50/abs(m) [1] 950 5 value of 1/X 1 1/2 1/3 1/4 1/5 1/6 probability 1/6 1/6 1/6 1/6 1/6 1/6 # Expected value m <- 1 * 1/6 + 1/2 * 1/6 + 1/3 * 1/6 + 1/4 * 1/6 + 1/5 * 1/6 + 1/6 * 1/6 m [1] 0.4083 1/3.5 [1] 0.2857 # Should we roll the dice? m > 1/3.5 [1] TRUE 6 a) Standard deviation of X: 2 b) Standard deviation of Y : 3 c) E(2X) = 48 d) Var(2X) = 44 e) E(X + Y ) = 24 + 22 = 26 f) Var(X + Y ) = 4 + 9 = 13 g) E(X Y ) = 24 22 = 2 h) What is Var(X Y ) = 4 + 9 = 13 7 a) E(2X) = 200 b) Var(2X) = (2 6) 2 = 4 36 = 144 c) E(X + Y ) = 100 + 90 = 190 Last Modified: April 2, 2012 d) Var(X + Y ) = 6 2 + 8 2 = 100 e) E(X Y ) = 100 90 = 10 f) Var(X Y ) = 6 2 + 8 2 = 100 Math 143 : Spring 2012 : Pruim
More About Random Variables 10.11 8 # Pr(X = 16) dbinom(16, 20, 0.8) [1] 0.2182 # Pr(X >= 16) 1 - pbinom(15, 20, 0.8) [1] 0.6296 E(X) = 20 0.8 = 16. Var(X) = 20 0.8 0.2 = 3.2. Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
10.12 More About Random Variables Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
Inference for One Mean 11.1 11 Inference for One Mean This is mostly covered in the text. Note that we are skipping section 11.5 at least for now. That sections covers how to compute a confidence interval for the variance, and you are welcome to read it if you like. Here is some R code related to t tests and confidence intervals. 11.1 The Long Way The test statistic for a null hypothesis of H 0 : µ = µ 0 is This is easily computed in RStudio: t = x µ 0 s/ n # some ingredients x.bar <- mean(humanbodytemp$temp); x.bar = estimate hypothesis value standard error [1] 98.52 sd <- sd (HumanBodyTemp$temp); sd [1] 0.6778 n <- nrow (HumanBodyTemp); n [1] 25 se <- sd/sqrt(n); se [1] 0.1356 # test statistic t <- ( x.bar - 98.6 ) / se; t Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
11.2 Inference for One Mean [1] -0.5606 # 2-sided p-value 2 * pt( - abs(t), df=24 ) [1] 0.5802 Similarly, we con compute a 95% confidence interval t.star <- qt(0.975, df = 24) t.star [1] 2.064 # lower limit x.bar - t.star * se [1] 98.24 # upper limit x.bar + t.star * se [1] 98.8 11.2 Super Short Cuts Of course, RStudio can do all of the calculations for you if you give it the raw data: t.test(humanbodytemp$temp, mu = 98.6) One Sample t-test data: HumanBodyTemp$temp t = -0.5606, df = 24, p-value = 0.5802 alternative hypothesis: true mean is not equal to 98.6 95 percent confidence interval: 98.24 98.80 sample estimates: mean of x 98.52 11.3 Confidence Intervals for Proportions Note: This topic is covered in section 7.3 of the textbook. Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
Inference for One Mean 11.3 The sampling distribution for a sample proportion (assuming samples of size n) is ( ) p(1 p) ˆp Norm p, n The standard deviation of a sampling distribution is called the standard error (abbreviated SE), so in this case we have p(1 p) SE = n This means that for 95% of samples, our estimated proportion ˆp will be within (1.96)SE of p. This tells us how well ˆp approximates p. It says that usually ˆp is between p 1.96SE and p + 1.96SE. Key idea: If ˆp is close to p, then p is close to ˆp. That is, for most samples p is between ˆp 1.96SE and ˆp + 1.96SE. Our only remaining difficulty is that we don t know SE because it depends on p, which we don t know. We will estimate SE as follows. Several methods have been proposed to work around this difficulty. Some of them work better than others. The Wald Interval The Old Traditional Method goes back to a statistician named Wald. Unfortunately, it is not very accurate when n is small (how large n must be depends on p), but it is simple: ˆp(1 ˆp) SE n ˆp(1 ˆp) We will call the interval between ˆp 1.96 n and ˆp + 1.96 we will write this very succinctly as ˆp ± 1.96SE ˆp(1 ˆp) n a 95% confidence interval for p. Often Notice that this fits our general pattern for confidence intervals where estimate ± (critical value)(standard error) estimate = ˆp = x n SE = ˆp(1 ˆp) n interval: ˆp ± z SE The Plus 4 Interval Recently, due to a paper by Agresti and Coull, a new method has become popular. It is almost as simple as the Wald Interval, and does a much better job of obtaining the desired 95% coverage rate. The idea is simple: Pretend you have 2 extra successes and two extra failures and use the Wald method with the modified data. The resulting method again fits our general pattern for confidence intervals with Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
11.4 Inference for One Mean estimate = p = x+2 SE = p (1 p ) n+4 n+4 interval: p ± z SE The addition of 2 and 4 comes from the fact that z = 1.96 2 and z 2 4 for a 95% confidence interval. It can safely be used for confidence levels between 90% and 99%, and that covers most confidence intervals used in practice. Example Q. Suppose we flip a coin 100 times and obtain 42 heads. What is a 95% confidence interval for p? Using the Wald method. ˆp = 42 100 =.42.42.58 SE = 100 = 0.049 z = 1.96 interval:.42 ± 1.96 0.049 or.42 ± 0.097 Using the Plus 4 method. p = 44 104 = 0.4231 p SE = (1 p ) 104 = 0.494 z = 1.96 interval: 0.4231 ± 1.96 0.048 or 0.4231 ± 0.968. Using R. Of course, R can do all of this for us. Both binom.test() and prop.test() report confidence intervals as well as p-values. The interval produced by prop.test() will be quite close to the one produced by the Plus 4 method. The interval produced by binom.test() will typically be a little bit wider because it guarantees to have a coverage rate of at least 95% whereas the one produced by prop.test() may have a coverage rate a bit above or below 95%. prop.test(42, 100) 1-sample proportions test with continuity correction data: x and n X-squared = 2.25, df = 1, p-value = 0.1336 alternative hypothesis: true p is not equal to 0.5 95 percent confidence interval: 0.3233 0.5229 sample estimates: p 0.42 Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
Inference for One Mean 11.5 binom.test(42, 100) Exact binomial test data: x and n number of successes = 42, number of trials = 100, p-value = 0.1332 alternative hypothesis: true probability of success is not equal to 0.5 95 percent confidence interval: 0.3220 0.5229 sample estimates: probability of success 0.42 Which denominator for SE? If you look in the book on page 162 you will see that it uses n 1 in the denominator for SEˆp. This is another variation on the theme, but it much less commonly used than any of the other methods discussed here. It performs about as well/poorly as the Wald method. Other confidence levels We can use any confidence level we like if we replace 1.96 with the appropriate critical value z. Example Q. Suppose we flip a coin 100 times and obtain 42 heads. What is a 99% confidence interval for p? A. The only thing that changes is our value of z which must now be selected so that 99% of a normal distribution is between z standard deviations below the mean and z standard deviations above the mean. R can calculate this value for us: qnorm(0.995) #.995 because we need.99 +.005 BELOW z* [1] 2.576 Now we proceed as before. SE =.42.58 100 = 0.049, so Wald confidence interval:.42 ± 2.576 0.049 or.42 ± 0.127. Plus 4 interval: 0.4231 ± 2.576 0.048 or 0.4231 ± 1.273. R computes these confidence intervals for us when we use prop.test() and binom.test() with the extra argument conf.level = 0.99. prop.test(42, 100, conf.level = 0.99) 1-sample proportions test with continuity correction Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
11.6 Inference for One Mean data: x and n X-squared = 2.25, df = 1, p-value = 0.1336 alternative hypothesis: true p is not equal to 0.5 99 percent confidence interval: 0.2973 0.5531 sample estimates: p 0.42 binom.test(42, 100, conf.level = 0.99) Exact binomial test data: x and n number of successes = 42, number of trials = 100, p-value = 0.1332 alternative hypothesis: true probability of success is not equal to 0.5 99 percent confidence interval: 0.2944 0.5535 sample estimates: probability of success 0.42 11.3.1 Determining Sample Size An important part of designing a study is deciding how large the sample needs to be for the intended purposes of the study. Q. You have been asked to conduct a public opinion survey to determine what percentage of the residents of a city are in favor of the mayor s new deficit reduction efforts. You need to have a margin of error of ±3%. How large must your sample be? (Assume a 95% confidence interval.) ˆp(1 ˆp) A. The margin of error will be 1.96 n, so our method will be to make a reasonable guess about what ˆp will be, and then determine how large n must be to make the margin of error small enough. Since SE is largest when ˆp = 0.50, one safe estimate for ˆp is 0.50, and that is what we will use unless we are quite sure that p is close to 0 or 1. (In those latter cases, we will make a best guess, erring on the side of being too close to 0.50 to avoid doing the work of getting a sample much larger than we need.) We can solve 0.03 = 0.5 0.5 n value of n. We could also graph method. 1.96 * sqrt(0.5 * 0.5/400) algebraically, or we can play a simple game of higher and lower until we get our 0.5 0.5 n and use the graph to estimate n. Here s the higher/lower guessing [1] 0.049 1.96 * sqrt(0.5 * 0.5/800) Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
Inference for One Mean 11.7 [1] 0.03465 1.96 * sqrt(0.5 * 0.5/1200) [1] 0.02829 1.96 * sqrt(0.5 * 0.5/1000) [1] 0.03099 1.96 * sqrt(0.5 * 0.5/1100) [1] 0.02955 1.96 * sqrt(0.5 * 0.5/1050) [1] 0.03024 So we need a sample size a bit larger than 1050, but not as large as 1100. We can continue this process to get a tighter estimate if we like: 1.96 * sqrt(0.5 * 0.5/1075) [1] 0.02989 1.96 * sqrt(0.5 * 0.5/1065) [1] 0.03003 1.96 * sqrt(0.5 * 0.5/1070) [1] 0.02996 1.96 * sqrt(0.5 * 0.5/1068) [1] 0.02999 1.96 * sqrt(0.5 * 0.5/1067) [1] 0.03 We see that a sample of size 1067 is guaranteed to give us a margin of error of at most 3%. It isn t really important to get this down to the nearest whole number, however. Our goal is to know roughly what size sample we need (tens? hundreds? thousands? tens of thousands?). Knowing the answer to 2 Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
11.8 Inference for One Mean significant figures is usually sufficient for planning purposes. Side note: The R function uniroot can automate this guessing for us, but it requires a bit of programming to use it: # uniroot finds when a function is 0, so we need to build such a # function f <- function(n) { 1.96 * sqrt(0.5 * 0.5/n) - 0.03 } # uniroot needs a function and a lower bound and upper bound to # search between uniroot(f, c(1, 50000))$root [1] 1067 Example Q. How would things change in the previous problem if 1. We wanted a 98% confidence interval instead of a 95% confidence interval? 2. We wanted a 95% confidence interval with a margin of error at most 0.5%? 3. We wanted a 95% confidence interval with a margin of error at most 0.5% and we are pretty sure that p < 10%? A. We ll use uniroot() here. You should use the higher-lower method or algebra and compare your results. f1 <- function(n) { qnorm(0.99) * sqrt(0.5 * 0.5/n) - 0.03 } uniroot(f1, c(1, 50000))$root [1] 1503 f2 <- function(n) { qnorm(0.975) * sqrt(0.5 * 0.5/n) - 0.005 } uniroot(f2, c(1, 50000))$root [1] 38415 f3 <- function(n) { qnorm(0.99) * sqrt(0.1 * 0.9/n) - 0.005 } uniroot(f3, c(1, 50000))$root [1] 19483 Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
Comparing Two Means 12.1 12 Comparing Two Means 12.1 Summary of Methods 12.1.1 Methods for One Variable So far, our inference methods have dealt with just one variable. We can divide the methods we know into three situations, depending on the type of variable we have: Type of Variable Parameter of Interest Method R function 1 Quantitative mean 1-sample t t.test() 1 Categorical (2 levels) proportion 1-proportion binom.test() prop.test() 1 Categorical ( 3 levels) multiple proportions Chi-squared goodness of fit chisq.test() Table 12.1: A summary of one variable statistical inference methods. For each row in Table 12.1, we have learned a method for computing p-values and for each row except the last we have a method for computing confidence intervals. These intervals (and several more that we will encounter) follow a common pattern: data value ± critical value standard error Quantitative Variable: data value: x (sample mean) critical value: t (using n 1 degrees of freedom) standard error: SE = s n. Categorial Variable (Wald Interval): data value: ˆp (sample proportion) critical value: z (using standard normal distribution) ˆp(1 ˆp) standard error: SE = n. For a Plus-4 interval use p = x+2 n+4 and n = n + 4 in place of ˆp and n. Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
12.2 Comparing Two Means There are methods (that we mostly won t cover in this course) for investigating other parameters (like median, standard deviation, etc.), so it is important to note that both the type of data and the parameter are important in determining the statistical method. 12.1.2 Methods for Two Variables Now we are ready to think about multiple variables. Let s start with two. If we have an explanatory variable and a response variable, we can map the type of data to the method of analysis as follows: Response Variable Categorical (2 levels) Categorical ( 3 levels) Chi-Squared for 2-way tables logistic regression Quantitative 2-sample t ANOVA simple linear regression paired t Cat (2 levels) Cat ( 3 levels) Quant Explanatory Variable Table 12.2: A summary of two variable statistical inference methods. We will learn about each of the methods in this table. We will also learn a little bit about methods that can handle more than two variables. As we go along, always keep in mind where we are in Table 12.2 12.2 Paired t Chapter 12 of our text covers two methods that are similar because the both use the t distribution, but otherwise really don t belong together since they are in different cells of Table 12.2. This means they are for situations where you have different kinds of data. 12.2.1 The paired t situation For a paired t test, we will have two quantitative measurements on the same scale for each observational unit. One common situation is a pre/post study where subjects are measured before and after some treatment, intervention, or time delay. Other examples of paired situations include: Measurements of husbands and wives on some quantitative scale (like satisfaction with their marriage). Measurements of speed of performance of a task with subjects left and right hands. Measurements of the distance a subject kicks two footballs, one filled with air, the other with helium. (Yes, this study has actually been done.) Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
Comparing Two Means 12.3 Crop studies where quantitative measures of crops (like yield per acre) are measured for crops experiencing two treatments (kinds of fertilizer, for example) if a portion of each plot gets each treatment. In this case, the plots become the observational units and for each plot we have two variables (yield with fertilizer A, yield with fertilizer B). Paired t procedures are just a 2-step version of 1-sample t procedures: Step 1: Combine the two measurements into one (usually by taking their difference). Step 2: Use a 1-sample t procedure on the new variable. 12.2.2 Blackbirds example The Blackbirds example compares the amount of antibodies in male blackbirds (on a log scale) before and after injection with testosterone. You can read the details of the study in the text. Here is the R code to compute the paired t test and confidence interval for this example. t.test(blackbirds$log.after - Blackbirds$log.before) # explicit subtraction One Sample t-test data: Blackbirds$log.after - Blackbirds$log.before t = 1.271, df = 12, p-value = 0.2277 alternative hypothesis: true mean is not equal to 0 95 percent confidence interval: -0.04008 0.15238 sample estimates: mean of x 0.05615 We can also give R two variables and let it do the subtraction for us by setting paired=true. The main advantage to this is that the output shows that you are doing a paired test. t.test(blackbirds$log.after, Blackbirds$log.before, paired = TRUE) # using paired = TRUE Paired t-test data: Blackbirds$log.after and Blackbirds$log.before t = 1.271, df = 12, p-value = 0.2277 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -0.04008 0.15238 sample estimates: mean of the differences 0.05615 If the data set did not have the log-transformed values already calculated, we could also do Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
12.4 Comparing Two Means t.test(log(blackbirds$after), log(blackbirds$before), paired = TRUE) Paired t-test data: log(blackbirds$after) and log(blackbirds$before) t = 1.244, df = 12, p-value = 0.2374 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -0.04135 0.15129 sample estimates: mean of the differences 0.05497 We could also calculate the p-values and confidence intervals from numerical summaries of the data m <- mean(log(after) - log(before), data=blackbirds); m [1] 0.05497 s <- sd(log(after) - log(before), data=blackbirds); s [1] 0.1594 n <- nrow(blackbirds); n # sample size [1] 13 SE <- s/ sqrt(n); SE # standard error [1] 0.04421 t <- ( m - 0 ) / SE; t # test statistic [1] 1.243 2 * pt( -abs(t), df=n-1 ) # 2-sided p-value [1] 0.2374 t.star <- qt(.975, df=n-1); t.star # critical value for 95% CI [1] 2.179 t.star * SE # margin of error Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
Comparing Two Means 12.5 [1] 0.09632 m - t.star * SE # lower bound on CI [1] -0.04135 m + t.star * SE # upper bound on CI [1] 0.1513 12.2.3 Side note: Paired t is a data reduction method Paired t procedures are an example of data reduction: we are combining multiple variables in our data set into one variable to make the analysis easier. There are many other examples of data reduction. One famous example in biology is body mass index (bmi). The formula used for bmi is body mass index = bmi = weight height 2 where weight is measured in kilograms and height in meters. This is a more complicated way of turning two measurements (height and weight) into a single measurement. i 12.3 Two Sample T 12.3.1 The 2-sample Method In a paired t situation we have two quantitative measurements for ech observational unit. In the Two Sample t situation we have only one quantitative measurement for each observational unit, but the observational units are in two groups. We use a categorical variable to indicate which group each observational unit is in. If we have two populations that are normal with means µ 1 and µ 2 and standard deviations σ 1 and σ 2, then the sampling distributions of the sample means using samples of sizes n 1 and n 2 are given by ( ) σ 1 X Norm µ 1, n1 ( ) σ 2 Y Norm µ 2, n2 So σ1 X Y Norm µ 2 1 µ 2, + σ2 2 n 1 n 2 So the 2-sample t procedures follow our typical pattern using data value: x y Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012
12.6 Comparing Two Means SE: σ 2 1 n 1 + σ2 2 n 2 degrees of freedom for t given by a messy formula but satisfying df min n 1, n 2 1 df n 1 + n 2 2 The degrees of freedom will be closer to the upper bound when the two standard deviations and the two sample sizes are close in value. 12.3.2 Horned Lizards This example is described in the text. Below is R code to compute the p-values and confidence intervals By Hand For each group we need the mean, standard deviation, and sample size. We can get them all at once using favstats() favstats(horn.length ~ group, data = HornedLizards) min Q1 median Q3 max mean sd n missing killed 15.2 21.1 22.25 23.8 26.7 21.99 2.709 30 0 living 13.1 23.0 24.55 26.0 30.3 24.28 2.631 154 1 We can use this information to compute SE: SE <- sqrt( 2.709^2 / 30 + 2.631^2 / 154 ); SE [1] 0.5381 To test H 0 : µ 1 µ 2 = 0, we use the test statistic t <- (21.99-24.28) / SE; t [1] -4.256 The degrees of freedom will be between 30 1 = 29 and 154 + 30 2 = 182, so our p-value (for a two-sided test) is between the two results below: 2 * pt(t, df = 29) [1] 0.0001991 2 * pt(t, df = 182) [1] 3.334e-05 Last Modified: April 2, 2012 Math 143 : Spring 2012 : Pruim
Comparing Two Means 12.7 In this example, the p-values are quite close and lead to the same conclusions. We can give a 95% confidence interval for the difference in the mean horn length using a margin of error computed as follows: t.star <- qt(.975, df=29); t.star [1] 2.045 MofE <- t.star * SE; MofE [1] 1.101 Super Short Cut Of course, R can automate the entire process: t.test(horn.length ~ group, HornedLizards) Welch Two Sample t-test data: horn.length by group t = -4.263, df = 40.37, p-value = 0.0001178 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -3.382-1.207 sample estimates: mean in group killed mean in group living 21.99 24.28 Notice that the degrees of freedom is indeed between 29 and 282. Math 143 : Spring 2012 : Pruim Last Modified: April 2, 2012