c:ms:multiple_regression_lecture_note_for_r
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
c:ms:multiple_regression_lecture_note_for_r [2024/06/05 10:33] – created hkimscil | c:ms:multiple_regression_lecture_note_for_r [2024/09/30 08:56] (current) – [Simple regression] hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Multiple regression with pr, spr, zero-order r ====== | ||
< | < | ||
# multiple regression: a simple e.g. | # multiple regression: a simple e.g. | ||
Line 9: | Line 10: | ||
colnames(d) <- c(" | colnames(d) <- c(" | ||
d | d | ||
- | attach(d) | + | # attach(d) |
lm.y.x1 <- lm(y ~ x1, data=d) | lm.y.x1 <- lm(y ~ x1, data=d) | ||
summary(lm.y.x1) | summary(lm.y.x1) | ||
Line 55: | Line 56: | ||
beta2 | beta2 | ||
- | install.packages(" | + | # install.packages(" |
library(lm.beta) | library(lm.beta) | ||
lm.beta(lm.y.x1x2) | lm.beta(lm.y.x1x2) | ||
Line 66: | Line 67: | ||
lm.tmp.2 <- lm(y~x1, data=d) | lm.tmp.2 <- lm(y~x1, data=d) | ||
- | res.y.x2 <- lm.tmp.2$residuals | + | res.y.x1 <- lm.tmp.2$residuals |
- | lm.tmp.3 <- lm(res.y.x2 ~ res.x2.x1, data=d) | + | lm.tmp.3 <- lm(res.y.x1 ~ res.x2.x1, data=d) |
summary(lm.tmp.3) | summary(lm.tmp.3) | ||
- | install.packages(" | + | # install.packages(" |
library(ppcor) | library(ppcor) | ||
pcor(d) | pcor(d) | ||
spcor(d) | spcor(d) | ||
- | partial.r <- pcor.test(y, | + | partial.r <- pcor.test(y, |
- | str(partial.r) | + | |
partial.r | partial.r | ||
+ | str(partial.r) | ||
partial.r$estimate^2 | partial.r$estimate^2 | ||
Line 85: | Line 86: | ||
lm.tmp.5 <- lm(y~x2, data=d) | lm.tmp.5 <- lm(y~x2, data=d) | ||
- | res.y.x1 <- lm.tmp.5$residuals | + | res.y.x2 <- lm.tmp.5$residuals |
- | lm.tmp.6 <- lm(res.y.x1 ~ res.x1.x2, data=d) | + | lm.tmp.6 <- lm(res.y.x2 ~ res.x1.x2, data=d) |
summary(lm.tmp.6) | summary(lm.tmp.6) | ||
- | partial.r2 <- pcor.test(y, | + | partial.r2 <- pcor.test(y, |
str(partial.r2) | str(partial.r2) | ||
partial.r2$estimate^2 | partial.r2$estimate^2 | ||
Line 104: | Line 105: | ||
spr.2$estimate^2 | spr.2$estimate^2 | ||
- | lm.tmp.7 <- lm(y~res.x2.x1, | + | lm.tmp.7 <- lm(y ~ res.x2.x1, data = d) |
summary(lm.tmp.7) | summary(lm.tmp.7) | ||
####################################################### | ####################################################### | ||
+ | # get the common area that explain the y variable | ||
+ | # 1. | ||
summary(lm.y.x2) | summary(lm.y.x2) | ||
all.x2 <- summary(lm.y.x2)$r.squared | all.x2 <- summary(lm.y.x2)$r.squared | ||
Line 113: | Line 116: | ||
all.x2 | all.x2 | ||
sp.x2 | sp.x2 | ||
- | all.x2 - sp.x2 | + | cma.1 <- all.x2 - sp.x2 |
+ | cma.1 | ||
+ | # 2. | ||
summary(lm.y.x1) | summary(lm.y.x1) | ||
all.x1 <- summary(lm.y.x1)$r.squared | all.x1 <- summary(lm.y.x1)$r.squared | ||
Line 120: | Line 125: | ||
all.x1 | all.x1 | ||
sp.x1 | sp.x1 | ||
- | all.x1 - sp.x1 | + | cma.2 <- all.x1 - sp.x1 |
+ | cma.2 | ||
+ | # OR 3. | ||
+ | summary(lm.y.x1x2) | ||
+ | r2.y.x1x2 <- summary(lm.y.x1x2)$r.square | ||
+ | r2.y.x1x2 | ||
+ | sp.x1 | ||
+ | sp.x2 | ||
+ | cma.3 <- r2.y.x1x2 - (sp.x1 + sp.x2) | ||
+ | cma.3 | ||
+ | |||
+ | cma.1 | ||
+ | cma.2 | ||
+ | cma.3 | ||
+ | # Note that sorting out unique and common | ||
+ | # explanation area is only possible with | ||
+ | # semi-partial correlation determinant | ||
+ | # NOT partial correlation determinant | ||
+ | # because only semi-partial correlation | ||
+ | # shares the same denominator (as total | ||
+ | # y). | ||
+ | ############################################# | ||
</ | </ | ||
+ | |||
+ | ====== Output ====== | ||
+ | ===== Multiple regression ===== | ||
+ | < | ||
+ | > | ||
+ | > lm.y.x1x2 <- lm(y ~ x1+x2, data=d) | ||
+ | > summary(lm.y.x1x2) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x1 + x2, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.2173 -0.5779 -0.1515 | ||
+ | |||
+ | Coefficients: | ||
+ | | ||
+ | (Intercept) | ||
+ | x1 | ||
+ | x2 -0.544727 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.9301 on 7 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | > | ||
+ | > lm.y.x1x2$coefficient | ||
+ | (Intercept) | ||
+ | | ||
+ | > # y.hat = 6.399103 + (0.011841)*x1 + (−0.544727)*x2 | ||
+ | > a <- lm.y.x1x2$coefficient[1] | ||
+ | > b1 <- lm.y.x1x2$coefficient[2] | ||
+ | > b2 <- lm.y.x1x2$coefficient[3] | ||
+ | > | ||
+ | > y.pred <- a + (b1 * x1) + (b2 * x2) | ||
+ | > y.pred | ||
+ | | ||
+ | | ||
+ | > y.real <- y | ||
+ | > y.real | ||
+ | | ||
+ | > y.mean <- mean(y) | ||
+ | > y.mean | ||
+ | [1] 8 | ||
+ | > | ||
+ | > res <- y.real - y.pred | ||
+ | > reg <- y.pred - y.mean | ||
+ | > ss.res <- sum(res^2) | ||
+ | > ss.reg <- sum(reg^2) | ||
+ | > | ||
+ | > ss.tot <- var(y) * (length(y)-1) | ||
+ | > ss.tot | ||
+ | [1] 30 | ||
+ | > ss.res | ||
+ | [1] 6.056235 | ||
+ | > ss.reg | ||
+ | [1] 23.94376 | ||
+ | > ss.res+ss.reg | ||
+ | [1] 30 | ||
+ | > | ||
+ | > # slope test | ||
+ | > summary(lm.y.x1x2) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x1 + x2, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.2173 -0.5779 -0.1515 | ||
+ | |||
+ | Coefficients: | ||
+ | | ||
+ | (Intercept) | ||
+ | x1 | ||
+ | x2 -0.544727 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.9301 on 7 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > # note on 2 t-tests | ||
+ | > | ||
+ | > # beta coefficient (standardized b) | ||
+ | > # beta <- b * (sd(x)/ | ||
+ | > beta1 <- b1 * (sd(x1)/ | ||
+ | > beta2 <- b2 * (sd(x2)/ | ||
+ | > beta1 | ||
+ | x1 | ||
+ | 0.616097 | ||
+ | > beta2 | ||
+ | x2 | ||
+ | -0.4458785 | ||
+ | > | ||
+ | > # install.packages(" | ||
+ | > library(lm.beta) | ||
+ | > lm.beta(lm.y.x1x2) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x1 + x2, data = d) | ||
+ | |||
+ | Standardized Coefficients:: | ||
+ | (Intercept) | ||
+ | | ||
+ | |||
+ | > | ||
+ | > ####################################################### | ||
+ | > # partial correlation coefficient and pr2 | ||
+ | > # x2's explanation? | ||
+ | > lm.tmp.1 <- lm(x2~x1, data=d) | ||
+ | > res.x2.x1 <- lm.tmp.1$residuals | ||
+ | > | ||
+ | > lm.tmp.2 <- lm(y~x1, data=d) | ||
+ | > res.y.x1 <- lm.tmp.2$residuals | ||
+ | > | ||
+ | > lm.tmp.3 <- lm(res.y.x1 ~ res.x2.x1, data=d) | ||
+ | > summary(lm.tmp.3) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = res.y.x1 ~ res.x2.x1, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.2173 -0.5779 -0.1515 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | res.x2.x1 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.8701 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | > # install.packages(" | ||
+ | > library(ppcor) | ||
+ | > pcor(d) | ||
+ | $estimate | ||
+ | y x1 x2 | ||
+ | y | ||
+ | x1 0.7825112 1.0000000 | ||
+ | x2 -0.6728560 0.3422911 | ||
+ | |||
+ | $p.value | ||
+ | y | ||
+ | y 0.00000000 0.01267595 0.04702022 | ||
+ | x1 0.01267595 0.00000000 0.36723388 | ||
+ | x2 0.04702022 0.36723388 0.00000000 | ||
+ | |||
+ | $statistic | ||
+ | | ||
+ | y | ||
+ | x1 3.325102 0.0000000 | ||
+ | x2 -2.406425 0.9638389 | ||
+ | |||
+ | $n | ||
+ | [1] 10 | ||
+ | |||
+ | $gp | ||
+ | [1] 1 | ||
+ | |||
+ | $method | ||
+ | [1] " | ||
+ | |||
+ | > spcor(d) | ||
+ | $estimate | ||
+ | y x1 x2 | ||
+ | y | ||
+ | x1 0.7171965 1.0000000 | ||
+ | x2 -0.6166940 0.2470028 | ||
+ | |||
+ | $p.value | ||
+ | y | ||
+ | y 0.00000000 0.113182 0.2748117 | ||
+ | x1 0.02964029 0.000000 0.5914441 | ||
+ | x2 0.07691195 0.521696 0.0000000 | ||
+ | |||
+ | $statistic | ||
+ | | ||
+ | y | ||
+ | x1 2.722920 0.0000000 | ||
+ | x2 -2.072679 0.6744045 | ||
+ | |||
+ | $n | ||
+ | [1] 10 | ||
+ | |||
+ | $gp | ||
+ | [1] 1 | ||
+ | |||
+ | $method | ||
+ | [1] " | ||
+ | |||
+ | > partial.r <- pcor.test(y, | ||
+ | > partial.r | ||
+ | | ||
+ | 1 -0.672856 0.04702022 -2.406425 10 1 pearson | ||
+ | > str(partial.r) | ||
+ | ' | ||
+ | $ estimate : num -0.673 | ||
+ | $ p.value | ||
+ | $ statistic: num -2.41 | ||
+ | $ n : int 10 | ||
+ | $ gp : num 1 | ||
+ | $ Method | ||
+ | > partial.r$estimate^2 | ||
+ | [1] 0.4527352 | ||
+ | > | ||
+ | > # x1's own explanation? | ||
+ | > lm.tmp.4 <- lm(x1~x2, data=d) | ||
+ | > res.x1.x2 <- lm.tmp.4$residuals | ||
+ | > | ||
+ | > lm.tmp.5 <- lm(y~x2, data=d) | ||
+ | > res.y.x2 <- lm.tmp.5$residuals | ||
+ | > | ||
+ | > lm.tmp.6 <- lm(res.y.x2 ~ res.x1.x2, data=d) | ||
+ | > summary(lm.tmp.6) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = res.y.x2 ~ res.x1.x2, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.2173 -0.5779 -0.1515 | ||
+ | |||
+ | Coefficients: | ||
+ | | ||
+ | (Intercept) 1.330e-17 | ||
+ | res.x1.x2 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.8701 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | > partial.r2 <- pcor.test(y, | ||
+ | > str(partial.r2) | ||
+ | ' | ||
+ | $ estimate : num 0.783 | ||
+ | $ p.value | ||
+ | $ statistic: num 3.33 | ||
+ | $ n : int 10 | ||
+ | $ gp : num 1 | ||
+ | $ Method | ||
+ | > partial.r2$estimate^2 | ||
+ | [1] 0.6123238 | ||
+ | > ####################################################### | ||
+ | > | ||
+ | > # semipartial correlation coefficient and spr2 | ||
+ | > # | ||
+ | > spr.1 <- spcor.test(y, | ||
+ | > spr.2 <- spcor.test(y, | ||
+ | > spr.1 | ||
+ | estimate | ||
+ | 1 -0.4086619 0.2748117 -1.184655 10 1 pearson | ||
+ | > spr.2 | ||
+ | | ||
+ | 1 0.5646726 0.113182 | ||
+ | > spr.1$estimate^2 | ||
+ | [1] 0.1670045 | ||
+ | > spr.2$estimate^2 | ||
+ | [1] 0.3188552 | ||
+ | > | ||
+ | > lm.tmp.7 <- lm(y ~ res.x2.x1, data = d) | ||
+ | > summary(lm.tmp.7) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ res.x2.x1, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.8617 -1.1712 -0.4940 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | res.x2.x1 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.767 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > ####################################################### | ||
+ | > | ||
+ | > # get the common area that explain the y variable | ||
+ | > # 1. | ||
+ | > summary(lm.y.x2) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x2, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.2537 -0.8881 -0.4851 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | x2 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.397 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > all.x2 <- summary(lm.y.x2)$r.squared | ||
+ | > sp.x2 <- spr.1$estimate^2 | ||
+ | > all.x2 | ||
+ | [1] 0.4792703 | ||
+ | > sp.x2 | ||
+ | [1] 0.1670045 | ||
+ | > cma.1 <- all.x2 - sp.x2 | ||
+ | > cma.1 | ||
+ | [1] 0.3122658 | ||
+ | > | ||
+ | > # 2. | ||
+ | > summary(lm.y.x1) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x1, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.5189 -0.8969 -0.1297 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) 3.617781 | ||
+ | x1 0.015269 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.176 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > all.x1 <- summary(lm.y.x1)$r.squared | ||
+ | > sp.x1 <- spr.2$estimate^2 | ||
+ | > all.x1 | ||
+ | [1] 0.631121 | ||
+ | > sp.x1 | ||
+ | [1] 0.3188552 | ||
+ | > cma.2 <- all.x1 - sp.x1 | ||
+ | > cma.2 | ||
+ | [1] 0.3122658 | ||
+ | > | ||
+ | > # OR 3. | ||
+ | > summary(lm.y.x1x2) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x1 + x2, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.2173 -0.5779 -0.1515 | ||
+ | |||
+ | Coefficients: | ||
+ | | ||
+ | (Intercept) | ||
+ | x1 | ||
+ | x2 -0.544727 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 0.9301 on 7 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > r2.y.x1x2 <- summary(lm.y.x1x2)$r.square | ||
+ | > r2.y.x1x2 | ||
+ | [1] 0.7981255 | ||
+ | > sp.x1 | ||
+ | [1] 0.3188552 | ||
+ | > sp.x2 | ||
+ | [1] 0.1670045 | ||
+ | > cma.3 <- r2.y.x1x2 - (sp.x1 + sp.x2) | ||
+ | > cma.3 | ||
+ | [1] 0.3122658 | ||
+ | > | ||
+ | > cma.1 | ||
+ | [1] 0.3122658 | ||
+ | > cma.2 | ||
+ | [1] 0.3122658 | ||
+ | > cma.3 | ||
+ | [1] 0.3122658 | ||
+ | > # Note that sorting out unique and common | ||
+ | > # explanation area is only possible with | ||
+ | > # semi-partial correlation determinant | ||
+ | > # NOT partial correlation determinant | ||
+ | > # because only semi-partial correlation | ||
+ | > # shares the same denominator (as total | ||
+ | > # y). | ||
+ | > ############################################# | ||
+ | > | ||
+ | > | ||
+ | > | ||
+ | </ | ||
+ | ====== Simple regression ====== | ||
+ | < | ||
+ | > # multiple regression: a simple e.g. | ||
+ | > # | ||
+ | > # | ||
+ | > rm(list=ls()) | ||
+ | > d <- read.csv(" | ||
+ | > d | ||
+ | | ||
+ | 1 6 220 5 | ||
+ | 2 5 190 6 | ||
+ | 3 7 260 3 | ||
+ | 4 7 200 4 | ||
+ | 5 8 330 2 | ||
+ | 6 | ||
+ | 7 8 210 3 | ||
+ | 8 | ||
+ | 9 9 320 1 | ||
+ | 10 | ||
+ | > | ||
+ | > colnames(d) <- c(" | ||
+ | > d | ||
+ | y x1 x2 | ||
+ | 1 6 220 5 | ||
+ | 2 5 190 6 | ||
+ | 3 7 260 3 | ||
+ | 4 7 200 4 | ||
+ | 5 8 330 2 | ||
+ | 6 10 490 4 | ||
+ | 7 8 210 3 | ||
+ | 8 11 380 2 | ||
+ | 9 9 320 1 | ||
+ | 10 9 270 3 | ||
+ | > # attach(d) | ||
+ | > lm.y.x1 <- lm(y ~ x1, data=d) | ||
+ | > summary(lm.y.x1) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x1, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.5189 -0.8969 -0.1297 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) 3.617781 | ||
+ | x1 0.015269 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.176 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | </ | ||
+ | |||
+ | 단순회귀분석에서 (simple regression) F-test와 t-test는 (slope test) 기본적으로 똑 같은 테스트를 말한다. 왜냐하면 F-test에 기여하는 독립변인이 오직하나이고 그 하나가 slope test에 (t-test) 사용되기 때문이다. 이것은 t-test의 t값과 F-test의 F값의 관계에서도 나타난다. | ||
+ | |||
+ | $$ t^2 = F $$ | ||
+ | |||
+ | < | ||
+ | > t.cal <- 3.7 | ||
+ | > t.cal^2 | ||
+ | [1] 13.69 | ||
+ | > F.cal <- 13.69 | ||
+ | > F.cal | ||
+ | [1] 13.69 | ||
+ | </ | ||
+ | |||
+ | Simple regression에서 설명한 것처럼 기울기에 (slope) 대한 t-test는 기울기가 y 변인의 variability를 (평균을 중심으로 흔들림을) 설명하는 데 기여했는가를 테스트 하기 위한 것이다. 기울기가 0 이라면 이는 평균을 (평균선이 기울기가 0이다) 사용하는 것과 같으므로 기울기의 효과가 없음을 의미한다. 따라서 b와 b zero의 차이가 통계학적으로 의미있었는가를 t-test한다. | ||
+ | $$ \text{t calculated value} = \frac {b - 0}{se} $$ | ||
+ | 위에서 $se$는 아래처럼 구한다고 언급하였다. | ||
+ | |||
+ | \begin{eqnarray*} | ||
+ | se & = & \sqrt{\frac{1}{n-2} * \frac{\text{SSE}}{\text{SSx}}} \\ | ||
+ | & = & \sqrt{\frac {\text{MSE}} {\text{SSx}}} \\ | ||
+ | \text{note that MSE } & = & \text{mean square error } \\ | ||
+ | & = & \text{ms.res } | ||
+ | \end{eqnarray*} | ||
+ | |||
+ | 위에서 구한 t값의 p value는 R에서 | ||
+ | < | ||
+ | summary(lm.y.x1) | ||
+ | n <- length(y) | ||
+ | k <- 1 # num of predictor variables | ||
+ | sse <- sum(lm.y.x1$residuals^2) # ss.res | ||
+ | ssx1 <- sum((x1-mean(x1))^2) | ||
+ | b <- lm.y.x1$coefficient[2] | ||
+ | se <- sqrt((1/ | ||
+ | t.b.cal <- (b - 0) / se | ||
+ | t.b.cal | ||
+ | p.value <- 2 * pt(t.b.cal, n-k-1, lower.tail=F) | ||
+ | p.value | ||
+ | # checck | ||
+ | t.b.cal | ||
+ | f.cal <- t.b.cal^2 | ||
+ | f.cal | ||
+ | p.value | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > summary(lm.y.x1) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x1, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.5189 -0.8969 -0.1297 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) 3.617781 | ||
+ | x1 0.015269 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.176 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > n <- length(y) | ||
+ | > k <- 1 # num of predictor variables | ||
+ | > sse <- sum(lm.y.x1$residuals^2) | ||
+ | > ssx1 <- sum((x1-mean(x1))^2) | ||
+ | > b <- lm.y.x1$coefficient[2] | ||
+ | > se < | ||
+ | > se < | ||
+ | > t.b.cal <- (b - 0) / se | ||
+ | > t.b.cal | ||
+ | x1 | ||
+ | 3.699639 | ||
+ | > p.value <- 2 * pt(t.b.cal, n-k-1, lower.tail=F) | ||
+ | > | ||
+ | > # checck | ||
+ | > t.b.cal | ||
+ | x1 | ||
+ | 3.699639 | ||
+ | > t.b.cal^2 | ||
+ | x1 | ||
+ | 13.68733 | ||
+ | > p.value | ||
+ | | ||
+ | 0.006045749 | ||
+ | > | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > | ||
+ | > lm.y.x2 <- lm(y ~ x2, data=d) | ||
+ | > summary(lm.y.x2) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x2, data = d) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -1.2537 -0.8881 -0.4851 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | x2 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 1.397 on 8 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | > | ||
+ | > | ||
+ | </ | ||
+ |
c/ms/multiple_regression_lecture_note_for_r.1717551215.txt.gz · Last modified: 2024/06/05 10:33 by hkimscil