c:ms:2023:schedule:w10.lecture.note
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
c:ms:2023:schedule:w10.lecture.note [2023/05/08 01:13] – created hkimscil | c:ms:2023:schedule:w10.lecture.note [2024/05/13 08:54] (current) – [R code] hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== R code ====== | ====== R code ====== | ||
< | < | ||
- | set.seed(101) | + | set.seed(401) |
- | x <- rnorm(400, 100, 10) | + | sn <- 25 |
+ | x <- rnorm(sn, 100, 10) | ||
x | x | ||
- | y <- 1.4*x + 2 + rnorm(400, 0, 4) | + | y <- 1.4 * x + 2 + rnorm(sn, 0, 10) |
y | y | ||
- | df <- data.frame(x, | + | df <- data.frame(x, |
# density graph | # density graph | ||
ggplot(data=df, | ggplot(data=df, | ||
Line 19: | Line 20: | ||
| | ||
coord_flip() | coord_flip() | ||
+ | |||
+ | lm.mod <- lm(y~x, data=df) | ||
+ | summary(lm.mod) | ||
+ | str(lm.mod) | ||
+ | inc.y <- lm.mod$coefficients[1] | ||
+ | slope.x <- lm.mod$coefficients[2] | ||
+ | inc.y | ||
+ | slope.x | ||
ggplot(data=df, | ggplot(data=df, | ||
- | geom_point(color=" | + | geom_point(color=" |
- | | + | |
- | | + | |
- | geom = " | + | |
- | set.seed(401) | + | ggplot(data=df, |
- | sn <- 25 | + | geom_point(color=" |
+ | geom_hline(aes(yintercept=mean(y)), | ||
+ | geom_abline(intercept=inc.y, | ||
+ | |||
+ | ################################ | ||
+ | ################################ | ||
+ | ################################ | ||
+ | ################################ | ||
+ | |||
+ | set.seed(101) | ||
+ | sn <- 400 | ||
x <- rnorm(sn, 100, 10) | x <- rnorm(sn, 100, 10) | ||
x | x | ||
- | y <- 1.4 * x + 2 + rnorm(sn, 0, 10) | + | y <- 1.4*x + 2 + rnorm(sn, 0, 16) |
y | y | ||
- | df <- data.frame(x, | + | df <- data.frame(x, |
# density graph | # density graph | ||
ggplot(data=df, | ggplot(data=df, | ||
Line 46: | Line 64: | ||
| | ||
coord_flip() | coord_flip() | ||
- | |||
- | lm.mod <- lm(y~x, data=df) | ||
- | summary(lm.mod) | ||
- | str(lm.mod) | ||
- | inc.y <- lm.mod$coefficients[1] | ||
- | slope.x <- lm.mod$coefficients[2] | ||
- | inc.y | ||
- | slope.x | ||
ggplot(data=df, | ggplot(data=df, | ||
geom_point(color=" | geom_point(color=" | ||
- | geom_hline(aes(yintercept=mean(y))) + | + | geom_hline(aes(yintercept=mean(y)), size=1, color=" |
- | | + | |
+ | formula | ||
+ | geom = " | ||
ggplot(data=df, | ggplot(data=df, | ||
- | geom_point(color=" | + | geom_point(color=" |
- | geom_hline(aes(yintercept=mean(y)), | + | geom_hline(aes(yintercept=mean(y)), |
- | geom_abline(intercept=inc.y, slope=slope.x, size=1.5, color=" | + | geom_abline(intercept=10, slope=1.5, size=1.5, color=" |
+ | |||
+ | lm.mod2 <- lm(y~x, data=df) | ||
+ | sum.lm.mod2 <- summary(lm.mod2) | ||
+ | sum.lm.mod2 | ||
+ | |||
+ | lm.mod2$coefficients[2] | ||
+ | lm.mod2$coefficients[1] | ||
+ | |||
+ | b <- lm.mod2$coefficients[2] | ||
+ | a <- lm.mod2$coefficients[1] | ||
+ | |||
+ | b | ||
+ | a | ||
+ | |||
+ | # y추정치 y.hat = a + bx | ||
+ | |||
+ | ggplot(data=df, | ||
+ | geom_point(color=" | ||
+ | geom_hline(aes(yintercept=mean(y)), | ||
+ | geom_abline(intercept=a, | ||
+ | |||
+ | lm.mod2$residuals | ||
+ | sum(lm.mod2$residuals) | ||
+ | ss.res <- sum(lm.mod2$residuals^2) | ||
+ | |||
+ | mean.y <- mean(df$y) | ||
+ | var.tot <- var(df$y) | ||
+ | df.tot <- length(df$y)-1 | ||
+ | ss.tot <- var.tot*df.tot | ||
+ | ss.tot | ||
+ | |||
+ | y.hat <- lm.mod2$fitted.values | ||
+ | # 참고로 | ||
+ | # y.hat2 <- a + b*x | ||
+ | head(y.hat) | ||
+ | head(y.hat2) | ||
+ | |||
+ | y.hat - mean(df$y) | ||
+ | explained <- y.hat - mean(df$y) | ||
+ | ss.exp <- sum(explained^2) | ||
+ | ss.exp | ||
+ | ss.res | ||
+ | |||
+ | ss.exp + ss.res | ||
+ | ss.tot | ||
+ | |||
+ | r.square <- ss.exp / ss.tot | ||
+ | r.square | ||
+ | sum.lm.mod2 | ||
+ | |||
+ | r.coeff <- sqrt(r.square) | ||
+ | r.coeff | ||
+ | cor(x,y) | ||
</ | </ | ||
+ | ====== R. output ====== | ||
+ | < | ||
+ | > set.seed(401) | ||
+ | > sn <- 25 | ||
+ | > x <- rnorm(sn, 100, 10) | ||
+ | > x | ||
+ | | ||
+ | [11] 95.16165 105.55799 100.47560 | ||
+ | [21] 114.26808 113.21215 110.42156 104.10994 107.89136 | ||
+ | > y <- 1.4 * x + 2 + rnorm(sn, 0, 10) | ||
+ | > y | ||
+ | [1] 147.7866 178.1177 167.8750 124.8276 147.9924 133.5853 144.6882 102.0537 140.3838 112.9193 125.8841 | ||
+ | [12] 135.8684 137.4363 129.0042 159.6048 137.0136 161.4669 147.8364 127.3562 122.0032 168.4221 138.2663 | ||
+ | [23] 147.7574 135.0859 153.9057 | ||
+ | > df <- data.frame(x, | ||
+ | > # density graph | ||
+ | > ggplot(data=df, | ||
+ | + | ||
+ | + | ||
+ | + color=" | ||
+ | + | ||
+ | `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. | ||
+ | > | ||
+ | > ggplot(data=df, | ||
+ | + | ||
+ | + | ||
+ | + color=" | ||
+ | + | ||
+ | > | ||
+ | > lm.mod <- lm(y~x, data=df) | ||
+ | > summary(lm.mod) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x, data = df) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -19.958 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | x | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 10.75 on 23 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > str(lm.mod) | ||
+ | List of 12 | ||
+ | $ coefficients : Named num [1:2] -5.92 1.45 | ||
+ | ..- attr(*, " | ||
+ | $ residuals | ||
+ | ..- attr(*, " | ||
+ | $ effects | ||
+ | ..- attr(*, " | ||
+ | $ rank : int 2 | ||
+ | $ fitted.values: | ||
+ | ..- attr(*, " | ||
+ | $ assign | ||
+ | $ qr :List of 5 | ||
+ | ..$ qr : num [1:25, 1:2] -5 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 ... | ||
+ | .. ..- attr(*, " | ||
+ | .. .. ..$ : chr [1:25] " | ||
+ | .. .. ..$ : chr [1:2] " | ||
+ | .. ..- attr(*, " | ||
+ | ..$ qraux: num [1:2] 1.2 1.24 | ||
+ | ..$ pivot: int [1:2] 1 2 | ||
+ | ..$ tol : num 1e-07 | ||
+ | ..$ rank : int 2 | ||
+ | ..- attr(*, " | ||
+ | $ df.residual | ||
+ | $ xlevels | ||
+ | $ call : language lm(formula = y ~ x, data = df) | ||
+ | $ terms :Classes ' | ||
+ | .. ..- attr(*, " | ||
+ | .. ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. .. ..$ : chr [1:2] " | ||
+ | .. .. .. ..$ : chr " | ||
+ | .. ..- attr(*, " | ||
+ | .. ..- attr(*, " | ||
+ | .. ..- attr(*, " | ||
+ | .. ..- attr(*, " | ||
+ | .. ..- attr(*, " | ||
+ | .. ..- attr(*, " | ||
+ | .. ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | $ model :' | ||
+ | ..$ y: num [1:25] 148 178 168 125 148 ... | ||
+ | ..$ x: num [1:25] 99 112.5 111.3 95.4 106.5 ... | ||
+ | ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. .. ..- attr(*, " | ||
+ | .. .. .. .. ..$ : chr [1:2] " | ||
+ | .. .. .. .. ..$ : chr " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. ..- attr(*, " | ||
+ | .. .. .. ..- attr(*, " | ||
+ | - attr(*, " | ||
+ | > inc.y <- lm.mod$coefficients[1] | ||
+ | > slope.x <- lm.mod$coefficients[2] | ||
+ | > inc.y | ||
+ | (Intercept) | ||
+ | | ||
+ | > slope.x | ||
+ | | ||
+ | 1.449211 | ||
+ | > | ||
+ | > ggplot(data=df, | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | > | ||
+ | > | ||
+ | > ggplot(data=df, | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | > | ||
+ | > ################################ | ||
+ | > ################################ | ||
+ | > ################################ | ||
+ | > ################################ | ||
+ | > | ||
+ | > set.seed(101) | ||
+ | > sn <- 400 | ||
+ | > x <- rnorm(sn, 100, 10) | ||
+ | > x | ||
+ | [1] 96.73964 105.52462 | ||
+ | [11] 105.26448 | ||
+ | | ||
+ | [31] 108.94937 102.79152 110.07866 | ||
+ | [41] 104.82459 107.58214 | ||
+ | | ||
+ | | ||
+ | [71] 115.09897 116.19937 111.53158 | ||
+ | [81] 118.52148 111.11675 | ||
+ | [91] 113.25470 | ||
+ | [101] 102.68066 | ||
+ | [111] 94.32079 | ||
+ | [121] 93.38395 | ||
+ | [131] 92.16866 102.44831 | ||
+ | [141] 90.90625 | ||
+ | [151] 93.81170 102.52963 | ||
+ | [161] 100.38116 103.94069 | ||
+ | [171] 104.51505 | ||
+ | [181] 109.15425 | ||
+ | [191] 102.02033 105.12656 114.52400 103.63865 | ||
+ | [201] 98.35968 | ||
+ | [211] 115.36571 | ||
+ | [221] 112.64838 102.69254 | ||
+ | [231] 107.69463 108.01970 | ||
+ | [241] 106.07012 | ||
+ | [251] 97.60977 | ||
+ | [261] 97.18546 | ||
+ | [271] 107.65473 | ||
+ | [281] 88.26203 | ||
+ | [291] 96.37157 114.12454 | ||
+ | [301] 88.34143 | ||
+ | [311] 102.39177 | ||
+ | [321] 103.91431 | ||
+ | [331] 97.75577 | ||
+ | [341] 99.58401 | ||
+ | [351] 96.70728 102.61308 | ||
+ | [361] 95.67570 | ||
+ | [371] 89.15158 100.60763 | ||
+ | [381] 102.26175 | ||
+ | [391] 103.06302 113.18370 | ||
+ | > y <- 1.4*x + 2 + rnorm(sn, 0, 16) | ||
+ | > y | ||
+ | [1] 132.30690 116.26373 102.92148 152.03568 148.62125 145.23458 128.72418 140.81982 180.13428 126.71891 | ||
+ | [11] 140.75356 126.70752 151.67496 140.90857 167.19847 138.26867 140.09513 127.80099 101.22968 | ||
+ | [21] 124.49299 138.55446 146.98584 147.30567 141.97227 121.66819 146.76882 160.20161 137.46257 130.32682 | ||
+ | [31] 137.63163 157.48389 161.87376 117.07605 170.70415 115.97677 168.13299 143.25823 120.14394 151.71639 | ||
+ | [41] 157.05003 147.87415 103.89740 137.70645 122.89482 161.45022 175.63248 124.98146 118.90559 125.17165 | ||
+ | [51] 113.71257 131.85370 122.43841 126.65040 126.63737 125.40707 149.53018 127.40238 150.91523 137.99144 | ||
+ | [61] 120.33013 115.81479 133.10345 154.33507 124.77469 129.11777 127.55762 121.86201 108.75323 149.03593 | ||
+ | [71] 175.48256 138.87299 176.42295 174.48624 113.85711 123.20494 158.95204 116.79922 128.38950 133.72567 | ||
+ | [81] 183.09255 173.82009 162.87062 151.14890 111.74930 156.72017 124.95247 158.56504 135.95007 183.62406 | ||
+ | [91] 159.29435 128.60357 121.00659 118.18438 130.73640 112.98394 133.61012 147.12472 153.10322 102.67619 | ||
+ | [101] 125.39519 123.09994 167.12412 149.23502 152.03587 142.92851 151.76348 102.75455 140.86985 161.79683 | ||
+ | [111] 152.06027 144.62060 139.54836 167.39453 154.49285 141.98107 117.93898 122.40050 123.26812 153.71793 | ||
+ | [121] 122.53166 136.58723 113.27211 154.18945 173.45069 126.17369 147.05687 149.68243 200.90285 100.62195 | ||
+ | [131] 148.73329 136.97472 162.00172 105.04047 152.31765 102.60661 170.85247 187.91323 147.18637 145.62508 | ||
+ | [141] 140.79521 108.84048 125.81853 126.80585 173.41216 166.45171 153.27515 120.51269 148.82013 122.36233 | ||
+ | [151] 117.01803 148.12499 141.53806 151.77987 136.56074 | ||
+ | [161] 108.67386 156.53995 149.56735 120.63470 143.62663 150.52260 128.25124 107.21147 161.80511 134.13153 | ||
+ | [171] 137.35287 164.00363 156.56514 153.90992 155.91806 147.69572 146.18249 146.38569 146.19223 135.74346 | ||
+ | [181] 146.02366 105.76156 139.97315 154.56684 152.23720 120.44186 135.72005 154.93419 150.38464 148.97005 | ||
+ | [191] 142.97852 160.25597 135.58243 149.65804 117.34308 143.64464 115.23345 172.41222 161.51218 160.49027 | ||
+ | [201] 133.47454 105.47177 141.60362 117.41335 148.44390 114.41137 143.26866 165.60948 148.84450 165.70723 | ||
+ | [211] 163.44605 116.68974 153.01589 131.20094 139.70546 168.97821 133.99155 127.38999 171.32937 144.22951 | ||
+ | [221] 164.24872 150.77626 164.65702 140.13966 135.62057 128.49762 137.64412 128.19980 136.26003 121.43766 | ||
+ | [231] 149.36358 150.85180 131.03067 148.53701 157.45806 153.61989 126.77119 136.71470 154.16814 138.81539 | ||
+ | [241] 147.06123 151.12100 | ||
+ | [251] 128.11498 120.18725 128.94675 143.67165 131.06991 | ||
+ | [261] 124.40672 137.60417 164.26852 139.64674 192.99892 146.51951 139.86154 152.50096 159.88686 130.82308 | ||
+ | [271] 153.38218 138.98802 154.75733 169.94912 109.17631 146.50361 146.73210 126.22433 135.90030 127.78037 | ||
+ | [281] 133.76225 | ||
+ | [291] 135.77740 164.45266 144.74040 138.94407 135.33135 131.65770 127.91329 155.32920 139.97307 150.81138 | ||
+ | [301] 126.12238 140.95811 154.78033 133.01955 145.56363 135.27575 175.07892 148.32427 145.09870 139.97886 | ||
+ | [311] 132.28771 154.59219 128.29751 159.62249 150.59405 111.25710 139.08130 186.21618 131.23893 159.99042 | ||
+ | [321] 140.45829 116.31435 123.08005 134.65362 141.07042 124.29293 137.22687 123.44081 | ||
+ | [331] 165.51711 134.46748 140.63536 142.15189 155.84088 142.25283 180.66935 133.44466 144.29671 119.22711 | ||
+ | [341] 157.47883 115.72553 131.52360 153.41154 158.16958 144.40760 145.22891 123.02795 120.76962 149.04446 | ||
+ | [351] 130.01780 147.60471 152.36399 156.37299 140.40351 | ||
+ | [361] 116.57163 158.37971 149.82117 142.89029 149.58717 160.62458 168.02319 182.00714 101.06283 | ||
+ | [371] 133.84178 150.09934 134.95391 126.05753 142.03125 143.39621 167.25819 135.76302 119.84304 145.65798 | ||
+ | [381] 155.74441 | ||
+ | [391] 136.34497 153.84423 137.19628 142.71654 155.03963 123.66223 128.68534 | ||
+ | > df <- data.frame(x, | ||
+ | > # density graph | ||
+ | > ggplot(data=df, | ||
+ | + | ||
+ | + | ||
+ | + color=" | ||
+ | + | ||
+ | `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. | ||
+ | > | ||
+ | > ggplot(data=df, | ||
+ | + | ||
+ | + | ||
+ | + color=" | ||
+ | + | ||
+ | > | ||
+ | > ggplot(data=df, | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + geom = " | ||
+ | > | ||
+ | > | ||
+ | > | ||
+ | > ggplot(data=df, | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | > | ||
+ | > lm.mod2 <- lm(y~x, data=df) | ||
+ | > sum.lm.mod2 <- summary(lm.mod2) | ||
+ | > sum.lm.mod2 | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x, data = df) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -48.386 -10.834 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | x 1.39426 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 14.66 on 398 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | > lm.mod2$coefficients[2] | ||
+ | | ||
+ | 1.394256 | ||
+ | > lm.mod2$coefficients[1] | ||
+ | (Intercept) | ||
+ | 0.9016802 | ||
+ | > | ||
+ | > b <- lm.mod2$coefficients[2] | ||
+ | > a <- lm.mod2$coefficients[1] | ||
+ | > | ||
+ | > ggplot(data=df, | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | > | ||
+ | > lm.mod2$residuals | ||
+ | 1 | ||
+ | | ||
+ | | ||
+ | -10.4955793 | ||
+ | | ||
+ | -27.6971994 -12.8688367 -13.5511384 -11.6514560 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | -24.1337116 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 0.4866024 -11.2459893 -14.2872615 -12.1019636 -13.5229133 -16.3214061 | ||
+ | 100 | ||
+ | -26.2963375 -18.6696323 | ||
+ | 109 | ||
+ | 7.0485934 | ||
+ | 118 | ||
+ | | ||
+ | 127 | ||
+ | | ||
+ | 136 | ||
+ | -12.3573364 | ||
+ | 145 | ||
+ | | ||
+ | 154 | ||
+ | 1.2427601 | ||
+ | 163 | ||
+ | | ||
+ | 172 | ||
+ | | ||
+ | 181 | ||
+ | | ||
+ | 190 | ||
+ | -15.3344050 | ||
+ | 199 | ||
+ | | ||
+ | 208 | ||
+ | | ||
+ | 217 | ||
+ | -11.1535346 -11.8233452 | ||
+ | 226 | ||
+ | | ||
+ | 235 | ||
+ | 8.9625719 | ||
+ | 244 | ||
+ | | ||
+ | 253 | ||
+ | -22.8078616 | ||
+ | 262 | ||
+ | | ||
+ | 271 | ||
+ | 2.3822204 | ||
+ | 280 | ||
+ | 9.3148527 | ||
+ | 289 | ||
+ | | ||
+ | 298 | ||
+ | 4.6101104 | ||
+ | 307 | ||
+ | | ||
+ | 316 | ||
+ | 5.3156858 | ||
+ | 325 | ||
+ | | ||
+ | 334 | ||
+ | 8.3563734 | ||
+ | 343 | ||
+ | 3.6590539 | ||
+ | 352 | ||
+ | 3.6341103 | ||
+ | 361 | ||
+ | -17.7264836 | ||
+ | 370 | ||
+ | -20.4484231 | ||
+ | 379 | ||
+ | -11.0169399 | ||
+ | 388 | ||
+ | 2.4075270 | ||
+ | 397 | ||
+ | | ||
+ | > sum(lm.mod2$residuals) | ||
+ | [1] -1.481315e-13 | ||
+ | > ss.res <- sum(lm.mod2$residuals^2) | ||
+ | > | ||
+ | > mean.y <- mean(df$y) | ||
+ | > var.tot <- var(df$y) | ||
+ | > df.tot <- length(df$y)-1 | ||
+ | > ss.tot <- var.tot*df.tot | ||
+ | > ss.tot | ||
+ | [1] 157720.1 | ||
+ | > | ||
+ | > y.hat <- lm.mod2$fitted.values | ||
+ | > y.hat - mean(df$y) | ||
+ | 1 | ||
+ | | ||
+ | 8 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 6.872098565 | ||
+ | | ||
+ | | ||
+ | | ||
+ | -31.979775017 | ||
+ | | ||
+ | -20.331471466 -15.679915057 | ||
+ | | ||
+ | 2.663423441 | ||
+ | | ||
+ | 1.925536197 | ||
+ | | ||
+ | | ||
+ | | ||
+ | -17.460253140 | ||
+ | | ||
+ | -23.748085276 | ||
+ | | ||
+ | | ||
+ | | ||
+ | 7.293378323 -10.997182870 | ||
+ | 106 | ||
+ | | ||
+ | 113 | ||
+ | | ||
+ | 120 | ||
+ | | ||
+ | 127 | ||
+ | -10.154276177 | ||
+ | 134 | ||
+ | -22.070852707 | ||
+ | 141 | ||
+ | -12.321426981 | ||
+ | 148 | ||
+ | | ||
+ | 155 | ||
+ | | ||
+ | 162 | ||
+ | 5.851921144 -20.613210572 -21.767732731 -12.568810769 | ||
+ | 169 | ||
+ | 0.337678648 -25.449786671 | ||
+ | 176 | ||
+ | | ||
+ | 183 | ||
+ | 1.920897652 | ||
+ | 190 | ||
+ | | ||
+ | 197 | ||
+ | | ||
+ | 204 | ||
+ | -10.663850175 | ||
+ | 211 | ||
+ | | ||
+ | 218 | ||
+ | | ||
+ | 225 | ||
+ | | ||
+ | 232 | ||
+ | | ||
+ | 239 | ||
+ | | ||
+ | 246 | ||
+ | | ||
+ | 253 | ||
+ | | ||
+ | 260 | ||
+ | | ||
+ | 267 | ||
+ | 3.181799384 | ||
+ | 274 | ||
+ | 5.934639390 -15.666422989 | ||
+ | 281 | ||
+ | -16.008148268 -29.595299776 | ||
+ | 288 | ||
+ | | ||
+ | 295 | ||
+ | 7.676609253 | ||
+ | 302 | ||
+ | | ||
+ | 309 | ||
+ | | ||
+ | 316 | ||
+ | -34.028292908 | ||
+ | 323 | ||
+ | -14.277169928 | ||
+ | 330 | ||
+ | 7.092908553 | ||
+ | 337 | ||
+ | 7.871933488 | ||
+ | 344 | ||
+ | 7.913192957 | ||
+ | 351 | ||
+ | | ||
+ | 358 | ||
+ | | ||
+ | 365 | ||
+ | | ||
+ | 372 | ||
+ | 1.204788347 | ||
+ | 379 | ||
+ | | ||
+ | 386 | ||
+ | 7.917140730 | ||
+ | 393 | ||
+ | | ||
+ | 400 | ||
+ | | ||
+ | > explained <- y.hat - mean(df$y) | ||
+ | > ss.exp <- sum(explained^2) | ||
+ | > ss.exp | ||
+ | [1] 72172.76 | ||
+ | > ss.res | ||
+ | [1] 85547.32 | ||
+ | > | ||
+ | > ss.exp + ss.res | ||
+ | [1] 157720.1 | ||
+ | > ss.tot | ||
+ | [1] 157720.1 | ||
+ | > | ||
+ | > r.square <- ss.exp / ss.tot | ||
+ | > r.square | ||
+ | [1] 0.4576003 | ||
+ | > sum.lm.mod2 | ||
+ | |||
+ | Call: | ||
+ | lm(formula = y ~ x, data = df) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -48.386 -10.834 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | x 1.39426 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 14.66 on 398 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > | ||
+ | > r.coeff <- sqrt(r.square) | ||
+ | > r.coeff | ||
+ | [1] 0.6764616 | ||
+ | > cor(x,y) | ||
+ | [1] 0.6764616 | ||
+ | > | ||
+ | </ | ||
+ | ====== R. Graph output ====== | ||
+ | |||
+ | {{: | ||
+ | {{: | ||
+ | {{: | ||
+ | {{: | ||
+ | |||
+ | |||
+ | {{: | ||
+ | {{: | ||
+ | {{: | ||
+ | {{: | ||
+ | |||
+ | ############################## | ||
+ | |||
+ | {{: | ||
+ | |||
c/ms/2023/schedule/w10.lecture.note.1683476010.txt.gz · Last modified: 2023/05/08 01:13 by hkimscil