§ 1.1
Which week corresponds to the highest percentage of ILI-related query fraction?
§ 1.2 Let us now understand the data at an aggregate level. Plot the histogram of the dependent variable, ILI. What best describes the distribution of values of ILI?
hist(FluTrain$ILI)
print("Most of the ILI values are small, with a relatively small number of much larger values")
[1] "Most of the ILI values are small, with a relatively small number of much larger values"
§ 1.3 Plot the natural logarithm of ILI versus Queries. What does the plot suggest?
§ 2.1 Based on the plot we just made, it seems that a linear regression model could be a good modeling choice. Based on our understanding of the data from the previous subproblem, which model best describes our estimation problem?
print("log(ILI) = intercept + coefficient x Queries, where the coefficient is positive")
[1] "log(ILI) = intercept + coefficient x Queries, where the coefficient is positive"
§ 2.2 What is the training set R-squared value for FluTrend1 model (the “Multiple R-squared”)?
FluTrend1 = lm(log(ILI)~Queries, data=FluTrain)
summary(FluTrend1)
Call:
lm(formula = log(ILI) ~ Queries, data = FluTrain)
Residuals:
Min 1Q Median 3Q Max
-0.76003 -0.19696 -0.01657 0.18685 1.06450
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.49934 0.03041 -16.42 <2e-16 ***
Queries 2.96129 0.09312 31.80 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.2995 on 415 degrees of freedom
Multiple R-squared: 0.709, Adjusted R-squared: 0.7083
F-statistic: 1011 on 1 and 415 DF, p-value: < 2.2e-16
print(0.709)
[1] 0.709
§ 2.3 What is the relationship we infer from our problem?
print("R-squared = Correlation^2")
[1] "R-squared = Correlation^2"
§ 3.1 What is our estimate for the percentage of ILI-related physician visits for the week of March 11, 2012?
PredTest1 = exp(predict(FluTrend1, newdata=FluTest))
PredTest1[which(FluTest$Week == "2012-03-11 - 2012-03-17")]
11
2.187378
§ 3.2 What is the relative error betweeen the estimate (our prediction) and the observed value for the week of March 11, 2012?
(2.293422 - 2.187378)/2.293422
[1] 0.04623833
§ 3.3 What is the Root Mean Square Error (RMSE) between our estimates and the actual observations for the percentage of ILI-related physician visits, on the test set?
RMSE
[1] 0.7490645
§ 4.1 How many values are missing in the new ILILag2 variable?
colSums(is.na(FluTrain))
Week ILI Queries ILILag2
0 0 0 2
§ 4.2
Use the plot() function to plot the log of ILILag2 against the log of ILI. Which best describes the relationship between these two variables?
plot(log(FluTrain$ILILag2), log(FluTrain$ILI))
print("There is a strong positive relationship between log(ILILag2) and log(ILI)")
[1] "There is a strong positive relationship between log(ILILag2) and log(ILI)"
§ 4.3 Which coefficients are significant at the p=0.05 level in this regression model? What is the R^2 value of the FluTrend2 model?
FluTrend2 = lm(log(ILI)~Queries+log(ILILag2), data=FluTrain)
summary(FluTrend2)
Call:
lm(formula = log(ILI) ~ Queries + log(ILILag2), data = FluTrain)
Residuals:
Min 1Q Median 3Q Max
-0.52209 -0.11082 -0.01819 0.08143 0.76785
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.24064 0.01953 -12.32 <2e-16 ***
Queries 1.25578 0.07910 15.88 <2e-16 ***
log(ILILag2) 0.65569 0.02251 29.14 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.1703 on 412 degrees of freedom
(2 observations deleted due to missingness)
Multiple R-squared: 0.9063, Adjusted R-squared: 0.9059
F-statistic: 1993 on 2 and 412 DF, p-value: < 2.2e-16
print("Intercept, Queries, log(ILILag2)")
[1] "Intercept, Queries, log(ILILag2)"
print(0.9063)
[1] 0.9063
§ 4.4
On the basis of R-squared value and significance of coefficients, which statement is the most accurate?
print("FluTrend2 is a stronger model than FluTrend1 on the training set.")
[1] "FluTrend2 is a stronger model than FluTrend1 on the training set."
§ 5.1 Modify the code from the previous subproblem to add an ILILag2 variable to the FluTest data frame. How many missing values are there in this new variable?
summary(FluTest$ILILag2)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
0.9018 1.1359 1.3409 1.5188 1.7606 3.6002 2
§ 5.2 Which value should be used to fill in the ILILag2 variable for the first observation in FluTest? Which value should be used to fill in the ILILag2 variable for the second observation in FluTest?
print("The ILI value of the second-to-last observation in the FluTrain data frame. ")
[1] "The ILI value of the second-to-last observation in the FluTrain data frame. "
print("The ILI value of the last observation in the FluTrain data frame.")
[1] "The ILI value of the last observation in the FluTrain data frame."
§ 5.3 What is the new value of the ILILag2 variable in the first row of FluTest? What is the new value of the ILILag2 variable in the second row of FluTest?
FluTest$ILILag2[1]
[1] 1.852736
FluTest$ILILag2[2]
[1] 2.12413
§ 5.4 What is the test-set RMSE of the FluTrend2 model?
RMSE
[1] 0.2942029
§ 5.5
Which model obtained the best test-set RMSE?
print("FluTrend2")
[1] "FluTrend2"