Introduction to Quantitative Methods

3. T-test for Difference in Means and Hypothesis Testing

3.2 Solutions

If you're using a UCL computer, please make sure you're running R version 3.3.1 or newer. Some of the seminar tasks and exercises will not work with older versions of R.

Exercise 1

Rename the variable wbgi_pse into pol.stability.

Solution

To do this we need to do two things. First, load the Stata data set and for that we need the foreign library. Second, we need to load the dplyr library in order to use rename().

library(foreign)
library(dplyr)
Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
df <- read.dta("QoG2012.dta")
df <- rename(df, pol.stability = wbgi_pse)

Exercise 2

Check whether political stability is different in countries that were former colonies (former_col == 1).

Solution

The variable former_col is binary, while pol.stability is continuous. Therefore, we use the t-test. Before we do this we declare former_col to be a factor variable. This will make it easier for you to interpret which group has the larger and which has the smaller mean.

df$former_col <- factor(df$former_col, labels = c("not ex colony", "ex colony"))
t.test(df$pol.stability ~ df$former_col, mu=0, alt="two.sided", conf=0.95)
    Welch Two Sample t-test

data:  df$pol.stability by df$former_col
t = 3.4674, df = 139.35, p-value = 0.0006992
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.2224004 0.8125053
sample estimates:
mean in group not ex colony     mean in group ex colony 
                  0.2858409                  -0.2316120 

Exercise 3

Choose an alpha level of .01.

Solution

The alpha level is the probability that we would see this data given that the null hypothesis was true. If this is very unlikely, we reject the null hypothesis. So, given an alpha level, your confidence-level that the alternative hypothesis is true is 1 - alpha level. This means with an alpha level of 0.01, you would set the argument conf = 0.99

t.test(df$pol.stability ~ df$former_col, mu=0, alt="two.sided", conf=0.99)
    Welch Two Sample t-test

data:  df$pol.stability by df$former_col
t = 3.4674, df = 139.35, p-value = 0.0006992
alternative hypothesis: true difference in means is not equal to 0
99 percent confidence interval:
 0.1277220 0.9071837
sample estimates:
mean in group not ex colony     mean in group ex colony 
                  0.2858409                  -0.2316120 

Exercise 4

We claim the difference in means is 0.3. Check this hypothesis.

Solution

Faced with the claim that the difference is 0.3, our alternative hypothesis would be that it is not 0.3. Therefore, the null hypothesis becomes that the difference in means is 0.3 and we check against that. To do this we set the argument mu=.3. If you run the code below you will see that the confidence interval includes 0.3. This means, we cannot reject the hypothesis that the difference in means is 0.3.

t.test(df$pol.stability ~ df$former_col, mu=0.3, alt="two.sided", conf=0.99)
    Welch Two Sample t-test

data:  df$pol.stability by df$former_col
t = 1.4571, df = 139.35, p-value = 0.1473
alternative hypothesis: true difference in means is not equal to 0.3
99 percent confidence interval:
 0.1277220 0.9071837
sample estimates:
mean in group not ex colony     mean in group ex colony 
                  0.2858409                  -0.2316120 

Exercise 5

Rename the variable lp_lat_abst into latitude.

Solution

df <- rename(df, latitude = lp_lat_abst)

Exercise 6

Check whether latitude and political stability are correlated.

Solution

We check for a relationship visually first.

plot(df$latitude, df$pol.stability)

Visual inspection from the scatterplot suggets a positive correlation. Positive in the sense that larger distances to the equator are related to more political stability. We now apply the appropriate test statistic, Pearson's r:

cor.test(df$latitude, df$pol.stability, use="complete.obs", conf.level = 0.99)
    Pearson's product-moment correlation

data:  df$latitude and df$pol.stability
t = 5.8247, df = 185, p-value = 2.492e-08
alternative hypothesis: true correlation is not equal to 0
99 percent confidence interval:
 0.2224487 0.5413167
sample estimates:
      cor 
0.3936597