r:linear_regression
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| r:linear_regression [2018/06/15 08:27] – [Ex 2] hkimscil | r:linear_regression [2019/06/13 10:15] (current) – hkimscil | ||
|---|---|---|---|
| Line 51: | Line 51: | ||
| Does the model fit the data well? | Does the model fit the data well? | ||
| * **Plot the residuals** and check the regression diagnostics. | * **Plot the residuals** and check the regression diagnostics. | ||
| + | * see [[https:// | ||
| Does the data satisfy the assumptions behind linear regression? | Does the data satisfy the assumptions behind linear regression? | ||
| * Check whether the diagnostics confirm that a linear model is reasonable for your data. | * Check whether the diagnostics confirm that a linear model is reasonable for your data. | ||
| Line 146: | Line 147: | ||
| </ | </ | ||
| + | <WRAP info> | ||
| + | What about beta coefficient? | ||
| + | |||
| + | < | ||
| + | |||
| + | < | ||
| + | EngineSize | ||
| + | -0.7100032 | ||
| + | > cor(MPG.city, | ||
| + | [1] -0.7100032 | ||
| + | > | ||
| + | </ | ||
| ====== Multiple Regression ====== | ====== Multiple Regression ====== | ||
| + | regression output table | ||
| | anova(m) | | anova(m) | ||
| | coefficients(m) = coef(m) | | coefficients(m) = coef(m) | ||
| Line 184: | Line 198: | ||
| F-statistic: | F-statistic: | ||
| </ | </ | ||
| - | Questions: | + | <WRAP box help>Questions: |
| * What is R< | * What is R< | ||
| * How many cars are involved in this test? (cf. df = 90) | * How many cars are involved in this test? (cf. df = 90) | ||
| + | * df + # of variables involved (3) = 93 | ||
| + | * check ' | ||
| * If I eliminate the R< | * If I eliminate the R< | ||
| + | </ | ||
| + | <WRAP box info>The last question: | ||
| + | * If I eliminate the R< | ||
| + | * to answer the question, use the regression output table: | ||
| + | |||
| + | R< | ||
| + | = | ||
| + | |||
| + | < | ||
| + | Analysis of Variance Table | ||
| + | |||
| + | Response: Cars93$MPG.city | ||
| + | Df Sum Sq Mean Sq F value Pr(> | ||
| + | Cars93$EngineSize | ||
| + | Cars93$Price | ||
| + | Residuals | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | > sstotal = 1465+131+1310 | ||
| + | > ssreg <- 1465+131 | ||
| + | > ssreg/ | ||
| + | [1] 0.54921 | ||
| + | > | ||
| + | > # or | ||
| + | > 1-(deviance(lm.model)/ | ||
| + | [1] 0.54932 | ||
| + | </ | ||
| + | |||
| + | |||
| + | </ | ||
| Regression formula: | Regression formula: | ||
| * $\hat{Y} = -2.99 * EngineSize + -0.15 * Price + 33.35$ | * $\hat{Y} = -2.99 * EngineSize + -0.15 * Price + 33.35$ | ||
| * $\hat{Y} = \widehat{\text{MPG.city}}$ | * $\hat{Y} = \widehat{\text{MPG.city}}$ | ||
| + | |||
| + | <WRAP box info>in the meantime, | ||
| + | < | ||
| + | Cars93$EngineSize | ||
| + | | ||
| + | > cor(MPG.city, | ||
| + | [1] -0.7100032 | ||
| + | > cor(EngineSize, | ||
| + | [1] 0.5974254 | ||
| + | > cor(MPG.city, | ||
| + | [1] -0.5945622 | ||
| + | > | ||
| + | </ | ||
| + | Or . . . . | ||
| + | < | ||
| + | > temp | ||
| + | . . . . | ||
| + | |||
| + | > cor(temp) | ||
| + | | ||
| + | MPG.city | ||
| + | EngineSize -0.7100032 | ||
| + | Price -0.5945622 | ||
| + | > | ||
| + | </ | ||
| + | Beta coefficients are not equal to correlations among variables. | ||
| + | </ | ||
| < | < | ||
| Line 452: | Line 526: | ||
| # Import data (simulated data for this example) | # Import data (simulated data for this example) | ||
| myData <- read.csv(' | myData <- read.csv(' | ||
| + | # or | ||
| + | # myData <- read.csv(' | ||
| - | # Build models | + | # Build models |
| m0 <- lm(happiness ~ 1, data=myData) | m0 <- lm(happiness ~ 1, data=myData) | ||
| m1 <- lm(happiness ~ age + gender, data=myData) | m1 <- lm(happiness ~ age + gender, data=myData) | ||
| Line 467: | Line 543: | ||
| Residuals 99 240.84 | Residuals 99 240.84 | ||
| </ | </ | ||
| - | |||
| - | < | ||
| - | Analysis of Variance Table | ||
| - | |||
| - | Model 1: happiness ~ age + gender | ||
| - | Model 2: happiness ~ age + gender + friends | ||
| - | Model 3: happiness ~ age + gender + friends + pets | ||
| - | Res.Df | ||
| - | 1 97 233.97 | ||
| - | 2 96 209.27 | ||
| - | 3 95 193.42 | ||
| - | --- | ||
| - | Signif. codes: | ||
| - | </ | ||
| - | |||
| - | * Model 0: SS< | ||
| - | * Model 1: SS< | ||
| - | * Model 2: SS< | ||
| - | * SS< | ||
| - | * FF(1,96) = 12.1293, pp = 0.0007521 (after adding friends) | ||
| - | * Model 3: SS< | ||
| - | * SS< | ||
| - | * FF(1,95) = 7.7828, pp = 0.0063739 (after adding pets) | ||
| < | < | ||
| Line 504: | Line 557: | ||
| Multiple R-squared: | Multiple R-squared: | ||
| F-statistic: | F-statistic: | ||
| + | </ | ||
| + | < | ||
| summary(m2) | summary(m2) | ||
| Line 519: | Line 573: | ||
| Multiple R-squared: | Multiple R-squared: | ||
| F-statistic: | F-statistic: | ||
| + | </ | ||
| + | < | ||
| summary(m3) | summary(m3) | ||
| Line 537: | Line 593: | ||
| </ | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | > lm.beta(m3) | ||
| + | age genderMale | ||
| + | -0.14098154 -0.04484095 | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | anova(m0, | ||
| + | Analysis of Variance Table | ||
| + | |||
| + | Model 1: happiness ~ 1 | ||
| + | Model 2: happiness ~ age + gender | ||
| + | Model 3: happiness ~ age + gender + friends | ||
| + | Model 4: happiness ~ age + gender + friends + pets | ||
| + | Res.Df | ||
| + | 1 99 240.84 | ||
| + | 2 97 233.97 | ||
| + | 3 96 209.27 | ||
| + | 4 95 193.42 | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | </ | ||
| + | |||
| + | * Model 0: SS< | ||
| + | * Model 1: SS< | ||
| + | * Model 2: SS< | ||
| + | * SS< | ||
| + | * F(1,96) = 12.1293, p value = 0.0007521 (after adding friends) | ||
| + | * Model 3: SS< | ||
| + | * SS< | ||
| + | * F(1,95) = 7.7828, p value = 0.0063739 (after adding pets) | ||
| + | |||
| + | |||
| {{https:// | {{https:// | ||
r/linear_regression.1529018865.txt.gz · Last modified: by hkimscil
