We have seen the use of analysis of variance tables in grouped and cross-classified experimental designs. However, their use is not restricted to these designs but applies to the whole class of linear models (more on this in Chapter 12).
The variation between and within groups for a one-way analysis of variance generalizes to model variation and residual variation
SSDmodel=
∑
model contains an intercept; see Section 12.2. The role of the group means in the one-way classification is taken over by the fitted values ˆyi in the more general linear model.An F test for significance of the model is available in direct analogy with Section 7.1. In simple linear regression, this test is equivalent to testing that the regression coefficient is zero.
The analysis of variance table corresponding to a regression analysis can be extracted with the function anova, just as for one- and two-way analyses of variance. For the thuesen example, it will look like this:
> attach(thuesen)
> lm.velo <- lm(short.velocity~blood.glucose)
> anova(lm.velo)
Analysis of Variance Table Response: short.velocity
Df Sum Sq Mean Sq F value Pr(>F) blood.glucose 1 0.20727 0.20727 4.414 0.0479 * Residuals 21 0.98610 0.04696
---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Notice that the F test gives the same p-value as the t test for a zero slope from Section 6.1. It is the same F test that gets printed at the end of the summaryoutput:
...
Residual standard error: 0.2167 on 21 degrees of freedom Multiple R-Squared: 0.1737, Adjusted R-squared: 0.1343 F-statistic: 4.414 on 1 and 21 DF, p-value: 0.0479
The remaining elements of the three output lines above may also be de-rived from the ANOVA table. “Residual standard error” is the square root of “Residual mean squares”, namely 0.2167=√0.04696. R2is the propor-tion of the total sum of squares explained by the regression line, 0.1737= 0.2073/(0.2073+0.9861); and, finally, the adjusted R2is the relative im-provement of the residual variance, 0.1343 = (v − 0.04696)/v, where v = (0.2073+0.9861)/22=0.05425 is the variance of short.velocity if the glucose values are not taken into account.
7.6 Exercises 143
7.6 Exercises
7.1 The zelazo data are in the form of a list of vectors, one for each of the four groups. Convert the data to a form suitable for the use of lm, and calculate the relevant test. Consider t tests comparing selected subgroups or obtained by combining groups.
7.2 In the lung data, do the three measurement methods give systemat-ically different results? If so, which ones appear to be different?
7.3 Repeat the previous exercises using the zelazo and lung data with the relevant nonparametric tests.
7.4 The igf1 variable in the juul data set is arguably skewed and has different variances across Tanner groups. Try to compensate for this us-ing logarithmic and square-root transformations, and use the Welch test.
However, the analysis is still problematic — why?
8
Tabular data
This chapter describes a series of functions designed to analyze tabular data. Specifically, we look at the functions prop.test, binom.test, chisq.test, and fisher.test.
8.1 Single proportions
Tests of single proportions are generally based on the binomial distribu-tion (see Secdistribu-tion 3.3) with size parameter N and probability parameter p.
For large sample sizes, this can be well approximated by a normal distri-bution with mean N p and variance N p(1 − p). As a rule of thumb, the approximation is satisfactory when the expected numbers of “successes”
and “failures” are both larger than 5.
Denoting the observed number of “successes” by x, the test for the hypothesis that p= p0can be based on
u= x − N p0 p Np0(1 − p0)
which has an approximate normal distribution with mean zero and stan-dard deviation 1, or on u2, which has an approximate χ2distribution with 1 degree of freedom.
P. Dalgaard, Introductory Statistics with R,
DOI: 10.1007/978-0-387-79054-1_8, © Springer Science+Business Media, LLC 2008
146 8. Tabular data
The normal approximation can be somewhat improved by the Yates correc-tion, which shrinks the observed value by half a unit towards the expected value when calculating u.
We consider an example (Altman, 1991, p. 230) where 39 of 215 randomly chosen patients are observed to have asthma and one wants to test the hypothesis that the probability of a “random patient” having asthma is 0.15. This can be done using prop.test:
> prop.test(39,215,.15)
1-sample proportions test with continuity correction data: 39 out of 215, null probability 0.15
X-squared = 1.425, df = 1, p-value = 0.2326
alternative hypothesis: true p is not equal to 0.15 95 percent confidence interval:
0.1335937 0.2408799 sample estimates:
p 0.1813953
The three arguments to prop.test are the number of positive outcomes, the total number, and the (theoretical) probability parameter that you want to test for. The latter is 0.5 by default, which makes sense for sym-metrical problems, but this is not the case here. The amount 15% is a bit synthetic since it is rarely the case that one has a specific a priori value to test for. It is usually more interesting to compute a confidence interval for the probability parameter, such as is given in the last part of the output.
Notice that we have a slightly unfortunate double usage of the symbol p as the probability parameter of the binomial distribution and as the test probability or p-value.
You can also use binom.test to obtain a test in the binomial distribution.
In that way, you get an exact test probability, so it is generally preferable to using prop.test, but prop.test can do more than testing single proportions. The procedure to obtain the p-value is to calculate the point probabilities for all the possible values of x and sum those that are less than or equal to the point probability of the observed x.
> binom.test(39,215,.15) Exact binomial test data: 39 and 215
number of successes = 39, number of trials = 215, p-value = 0.2135 alternative hypothesis: true probability ... not equal to 0.15 95 percent confidence interval:
0.1322842 0.2395223 sample estimates:
probability of success 0.1813953
The “exact” confidence intervals at the 0.05 level are actually constructed from the two one-sided tests at the 0.025 level. Finding an exact confi-dence interval using two-sided tests is not a well-defined problem (see Exercise 8.5).