## 2.2 Solutions

### 2.2.1 Exercise 2

Use the names() function to display the variable names of the longley dataset.

names(longley)
 "GNP.deflator" "GNP"          "Unemployed"   "Armed.Forces"
 "Population"   "Year"         "Employed"    

### 2.2.2 Exercise 3

Use square brackets to access the 4th column of the dataset.

longley[, 4]
  159.0 145.6 161.6 165.0 309.9 359.4 354.7 335.0 304.8 285.7 279.8
 263.7 255.2 251.4 257.2 282.7

### 2.2.3 Exercise 4

Use the dollar sign to access the 4th column of the dataset.

x = longley$Population, # x-axis variable type = "l", # produce a line plot bty = "n", # no box around our plot main = "Relationship of Population Size and Size of the Economy", xlab = "Population older than 14 years of age", ylab = "Gross national product" ) ### 2.2.7 Exercise 8 Create a boxplot showing the distribution of IMMBRIT by each party in the data and plot these in one plot next to each other. To do that, we load the non-western foreigners dataset first. Note: You have to set your working directory that R operates in to the location of the dataset. # load perception of non-western foreigners data load("BSAS_manip.RData") We have five parties in our dataset. We plot 5 boxplots next to each other. Hence, we separate the plot window into 1 row and 5 columns. # plot window to 1 row and 5 columns par(mfrow = c(1, 5)) boxplot(data2$IMMBRIT[ data2$Cons == 1 ], frame.plot = FALSE, col = "blue", xlab = "Tories") boxplot(data2$IMMBRIT[ data2$Lab == 1 ], frame.plot = FALSE, col = "red", xlab = "Labour") boxplot(data2$IMMBRIT[ data2$SNP == 1 ], frame.plot = FALSE, col = "yellow", xlab = "SNP") boxplot(data2$IMMBRIT[ data2$Ukip == 1 ], frame.plot = FALSE, col = "purple", xlab = "Ukip") boxplot(data2$IMMBRIT[ data2$BNP == 1 ], frame.plot = FALSE, col = "darkblue", xlab = "BNP") ### 2.2.8 Exercises 9 and 10 We combine the answer to questions 9 and 10. Question from 9: Is there a difference between women and men in terms of their subjective estimation of foreingers? Question from 10: What is the difference between women and men? Women’s subjective estimate is the mean of IMMBRIT across women and equally, men’s subjective estimate is the mean of IMMBRIT over all men. Let’s get these numbers with the mean function and the square brackets. womens.mean <- mean(data2$IMMBRIT[ data2$RSex == 2 ]) womens.mean  32.79159 mens.mean <- mean(data2$IMMBRIT[ data2\$RSex == 1 ])
mens.mean
 24.53766

The difference between women and men is the difference in means. Let’s take the difference between them. The difference in means is often referred to as the first difference.

first.difference <- womens.mean - mens.mean
first.difference
 8.253937

Let’s round that number. We don’t like to see so many decimal places. You should usually present precision up to the second decimal place. We can use the round() function. The first argument is number to round and the second is the amount of digits.

round(first.difference, 2)
 8.25

We do find a difference between men and women. On average, women’s estimate of the number of non-western foreingers is 8.25 greater than men’s estimate.

At this point we have established that there is a difference in our sample. Samples are subject to sampling variability. That means, we cannot yet say that the difference is systematic, i.e., British women, generally, think that there are more non-western foreingers than British men.

### 2.2.9 Exercises 11

Could you form a hypothesis out of the relationship that you see if any exists?

Our testable hypothesis could be: Women tend to overestimate the number of foreigners more than men. In our sample, women tend to estimate on the number of foreingers at