Finance & Regression: Exercises


5. Industry Sector and Stock Returns. You are an equity analyst looking to examine the association between industry sector and stock returns. You have collected monthly return data for several stocks from different industry sectors: Technology, Healthcare, and Energy. The dataset includes the returns for each stock and its corresponding sector.

In this exercise, you'll run a regression to understand how much of the returns can be explained by the different sectors. To do this, you will apply the factor() function to the sector column when including it in the regression, lm().

When you use summary() to examine your regression results, you will notice that one sector has been excluded from the regression. You can interpret the coefficients on the other sectors as the expected difference in returns between those sectors and the excluded sector.


# Pre-load the dataset for the exercise set.seed(123) returns <- rnorm(150, mean = 0.05, sd = 0.10) sector <- sample(c("Technology", "Healthcare", "Energy"), 150, replace = TRUE) dataset <- data.frame(returns, sector) # View the first few rows of the dataset head(dataset) # Run a regression of returns on factorized sector model <- lm(___ ~ ___(sector), data = dataset) # Print the model summary ___(model) # View the first few rows of the dataset head(dataset) # Run a regression of returns on factorized sector model <- lm(returns ~ factor(sector), data = dataset) # Print the model summary summary(model) test_error() test_object("model", incorrect_msg="Did you correctly define the regression model?") test_function("lm", incorrect_msg="Make sure you used the `lm()` function for the regression.") test_function("summary", args="object", incorrect_msg="Did you print the summary of the regression model?") success_msg("Well done! Notice the impact of each sector on the stock returns.")
Use the `lm()` function to create the regression model. Make sure to convert the `sector` column to a factor using `factor()` within the `lm()` function.

Previous Exercise Next Exercise