1
df <- read.csv('week 6 data-1.csv')
corr <- cor(df$RVUs,df$Expenditures)
corr
## [1] 0.9217239
*** 0.9217239 which indicates a strong positive relationship between RVUs and Expenditures***
2
model1 <- lm(Expenditures ~ RVUs, df)
summary(model1)
##
## Call:
## lm(formula = Expenditures ~ RVUs, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -185723026 -14097620 2813431 11919781 642218316
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.785e+06 4.413e+06 -0.858 0.392
## RVUs 2.351e+02 5.061e+00 46.449 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 67350000 on 382 degrees of freedom
## Multiple R-squared: 0.8496, Adjusted R-squared: 0.8492
## F-statistic: 2157 on 1 and 382 DF, p-value: < 2.2e-16
par(mfrow=c(2,2)) # Change the panel layout to 2 x 2
plot(model1)
par(mfrow=c(1,1)) # Change back to 1 x 1
*** beta0 = -3.785e+06 but it is not statistically significant beta1 = 2.351e+02 and it is significant at 0.001 level *** beta1 indicates the positive relationship between two variables R-sqaured is 0.8496 meanning around 85% y is explained by x F stats and p-value tells us the model is statistically significant y = -3.785e+06 + 2.351e+02x + error If you change x by 1, y will change by 2.351e+02 in same direction The result should be valid and similar to real life case Investigate of assumptions: 1) linearity: no 2) Error independent: no 3) Homoscedasticity: no 4) Residual normal distributed: Yes
2
df$log_e <- log(df$Expenditures)
model2 <- lm(log_e ~ RVUs, df)
summary(model2)
##
## Call:
## lm(formula = log_e ~ RVUs, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.59439 -0.29504 0.06135 0.35333 1.20871
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.730e+01 3.325e-02 520.11 <2e-16 ***
## RVUs 1.349e-06 3.814e-08 35.38 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5076 on 382 degrees of freedom
## Multiple R-squared: 0.7661, Adjusted R-squared: 0.7655
## F-statistic: 1251 on 1 and 382 DF, p-value: < 2.2e-16
Intercept 1.730e+01 and it is statistically significant Beta1 is 1.349e-06, positive relationship and statistically significant. We change x by 1 unit, we expect y change by 100*1.349e-06 percent in same direction R-square is 0.7661 which is not as good as previous one but still good fit. F-stat and p-value shows the significance of the model. ln(y) = 1.730e+01 + 1.349e-06x + error Investigate of assumptions: 1) linearity: yes 2) Error independent: yes 3) Homoscedasticity: yes 4) Residual normal distributed: yes
par(mfrow=c(2,2)) # Change the panel layout to 2 x 2
plot(model2)
par(mfrow=c(1,1)) # Change back to 1 x 1
1 When we apply log, it will make distribution more linear. It will affect the relationship to linear relationship which satisfy the gaussian assumption. The log process is very similar to normalization which will make the error, everything follow the normal distribution more closely which prevent error go so far. And the variance will follow homoscedasticity which is consistent.
2 I am satisfied with current model (good R-sqaure) but I will not use it in my research or projects. First, as I mentioned, the log transformation does not improve R-Square and we may try some other method such as quadratic term, reciprocal mentioned in the question, standarization, normalization and so on. But even I tried those, I will not be likely to choose linear models because they are too simple to handle real life cases. Advanced model such as random forest, XGboost, neural network might be my choice to invest all of my money.