Exercises D

Programming and Data Analysis with R

Author
Affiliation

Tommaso Rigon

UniversitĂ  degli Studi di Milano-Bicocca

Homepage

These exercises accompany Units K and L.

The exercises whose title carries a number such as (AK 6.15) are taken, sometimes in a simplified form, from Agresti and Kateri (2022); their data are those of the website of the book; a copy of each file is available on this website, at the link given in each exercise.

The solution of each exercise is in a separate R script, linked from the title of the exercise itself.

Linear regression

Let us go back to the forbes data of Unit F: the boiling temperature of water and the atmospheric pressure at 17 locations in the Alps. As in Unit F, convert the temperature into degrees Celsius.


  1. Fit the simple linear regression model with response Pressure and explanatory variable TempC using lm. Check that the estimates are those obtained in Unit F, and interpret the slope.

  2. Is the temperature related to the pressure? Test H_0: \beta_1 = 0 and compute the 95\% confidence interval for \beta_1.

  3. Water boils at 97 degrees Celsius. Compute the confidence interval for the mean pressure and the prediction interval for the pressure at that location. Which one is wider, and why?

  4. Plot the residuals against the fitted values. Is there any location with a much larger residual than the others?

The Houses data of Exercises B are available at this link. Consider the selling price (price, thousands of dollars), the size (size, square feet) and the annual tax bill (taxes, dollars).


  1. Fit the model with response price and explanatory variable size. Interpret the slope and its 95\% confidence interval.

  2. Compute the confidence interval for the mean price of the houses of 2000 square feet, and the prediction interval for the price of a single house of 2000 square feet.

  3. Add taxes to the model. How does the effect of size change? Why? Hint: compute the correlation between size and taxes.

The UN data of Exercises B are available at this link. Consider the carbon dioxide emissions per capita (CO2, metric tons), the per capita gross domestic product (GDP, thousands of dollars) and the percentage of people using the Internet (Internet).


  1. Fit the model with response CO2 and explanatory variable GDP. Is the effect of GDP statistically significant?

  2. Add Internet to the model. Are the two variables statistically significant? What does the F test in the last line of summary say?

  3. The results of point 2 may look contradictory. Hint: compute the correlation between GDP and Internet, and recall that each t test asks whether a variable is useful given the other one.

  4. Compare the values of R^2 of the two models. Does Internet improve the fit appreciably?

Qualitative explanatory variables and ANOVA

The Income data of Unit L are available at this link: the annual income (income, thousands of dollars), the years of education (education) and the racial-ethnic group (race: B, H, W) of 80 adult Americans.


  1. Fit the model with response income and explanatory variable education. Interpret the slope and its 95\% confidence interval.

  2. Convert race into a factor with Hispanics as the reference category, using factor(Income$race, levels = c("H", "B", "W")). Fit the model income ~ race: which coefficient is the difference between the mean incomes of Whites and Hispanics? Is it significant at the 5\% level?

  3. Fit the model income ~ race + education and compute, with predict, the predicted income of a Black, a Hispanic and a White person with 12 years of education. How do the differences between the three predictions relate to the coefficients of the model?

The Polid data file of Agresti and Kateri (2022), available at this link, reports the political ideology (ideology, from 1 = extremely liberal to 7 = extremely conservative) and the racial-ethnic group (race) of 2575 respondents to the General Social Survey.


  1. Import the data and convert race into a factor. How many respondents are there in each group? Compute the mean ideology of each group.

  2. Fit the model ideology ~ race. Which is the reference category? Check that the coefficients are the differences between the means of point 1.

  3. Use anova to test whether the three groups have the same mean ideology. Report the F value and the P-value, and draw a conclusion.

  4. The largest difference between the means is about 0.35 points on a scale from 1 to 7. Is it large in practice? Why is it nonetheless statistically significant?

References

Agresti, A., and Kateri, M. (2022), Foundations of statistics for data scientists, CRC Press.