Group Exercise: Confidence Interval

We can estimate the confidence interval for a mean using the t.test function. You need, of course, a numeric variable to calculate a mean. Identify the variable and a population value, mu, to test for (which should be derived from theory or make sense at face value). In the World95 data, we can use the percent population change variable to test for a population increase.

> t.test(pop_incr, mu=0)

This results in:

One Sample t-test

data: pop_incr
t = 14.667, df = 108, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
1.455019 1.909752
sample estimates:
mean of x

We can also test for a population proportion, using the prop.test function. In this example, we'll use the variable Q2 from the CBS2011 dataframe. (This variable asks respondents if they think the country is going in the right direction, 1, or the wrong direction, 2.)

We can see the table with the CrossTable function.

> CrossTable(Q2)

Which gives us:
Cell Contents
| N |
| N / Table Total |

Total Observations in Table: 966

| 1 | 2 |
| 292 | 674 |
| 0.302 | 0.698 |

Now we can test the proportion of respondents who said "right direction" (1). The syntax here is a bit complicated because we're nesting functions:

> prop.test(length(na.omit(Q2[Q2==1])),length(na.omit(Q2)))

We're using three different functions. The innermost function is na.omit, which tells R to drop cases coded as 'NA' -- that is, to treat them as missing. When we say na.omit(Q2), we are asking for a version of Q2 without the cases coded 'NA'. When we say na.omit(Q2[Q2==1]), we are asking for the cases for respondents who answered 1 to Q2 -- that is what [Q2==1] specifies. The length() function calculates the count of cases for the variable specified. The prop.test function evaluates the number of cases of respondents who answered '1' -- length(na.omit(Q2[Q2==1])) -- compared to the total number of cases for Q2 -- length(na.omit(Q2)).

The result is:
1-sample proportions test with continuity correction

data: length(na.omit(Q2[Q2 == 1])) out of length(na.omit(Q2)), null probability 0.5
X-squared = 150.2702, df = 1, p-value < 2.2e-16
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
0.2736455 0.3325036
sample estimates:

By default, we test pi == 0.5, which is the proportion we would expect due to chance.

Group Exercise
Calculate a confidence interval for a mean or a proportion and interpret the results. Indicate in your comment the dataframe and variable name and the population parameter you tested. Remember to include everyone's name.