Exercises D
Programming and Data Analysis with R
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.
Fit the simple linear regression model with response
Pressureand explanatory variableTempCusinglm. Check that the estimates are those obtained in Unit F, and interpret the slope.Is the temperature related to the pressure? Test H_0: \beta_1 = 0 and compute the 95\% confidence interval for \beta_1.
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?
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).
Fit the model with response
priceand explanatory variablesize. Interpret the slope and its 95\% confidence interval.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.
Add
taxesto the model. How does the effect ofsizechange? Why? Hint: compute the correlation betweensizeandtaxes.
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).
Fit the model with response
CO2and explanatory variableGDP. Is the effect ofGDPstatistically significant?Add
Internetto the model. Are the two variables statistically significant? What does the F test in the last line ofsummarysay?The results of point 2 may look contradictory. Hint: compute the correlation between
GDPandInternet, and recall that each t test asks whether a variable is useful given the other one.Compare the values of R^2 of the two models. Does
Internetimprove 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.
Fit the model with response
incomeand explanatory variableeducation. Interpret the slope and its 95\% confidence interval.Convert
raceinto afactorwith Hispanics as the reference category, usingfactor(Income$race, levels = c("H", "B", "W")). Fit the modelincome ~ race: which coefficient is the difference between the mean incomes of Whites and Hispanics? Is it significant at the 5\% level?Fit the model
income ~ race + educationand compute, withpredict, 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.
Import the data and convert
raceinto afactor. How many respondents are there in each group? Compute the mean ideology of each group.Fit the model
ideology ~ race. Which is the reference category? Check that the coefficients are the differences between the means of point 1.Use
anovato test whether the three groups have the same mean ideology. Report theF valueand the P-value, and draw a conclusion.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?