c:ms:2023:schedule:w10.lecture.note
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
c:ms:2023:schedule:w10.lecture.note [2023/05/08 01:15] – 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(101) | ||
- | > x <- rnorm(400, 100, 10) | ||
- | > x | ||
- | [1] 96.73964 105.52462 | ||
- | [8] 98.87266 109.17028 | ||
- | | ||
- | [22] 107.08522 | ||
- | [29] 104.67239 104.98136 108.94937 102.79152 110.07866 | ||
- | | ||
- | | ||
- | | ||
- | [57] 101.65381 111.29809 111.73722 | ||
- | [64] 101.12458 104.22604 103.86835 | ||
- | [71] 115.09897 116.19937 111.53158 | ||
- | | ||
- | | ||
- | | ||
- | [99] 104.97454 | ||
- | [106] 97.69491 100.87772 | ||
- | [113] 98.43019 116.02242 107.68654 | ||
- | [120] 109.81085 | ||
- | [127] 92.46059 | ||
- | [134] 83.91369 109.51580 | ||
- | [141] 90.90625 | ||
- | [148] 95.29929 | ||
- | [155] 95.97413 | ||
- | [162] 103.94069 | ||
- | [169] 99.98572 | ||
- | [176] 112.18073 | ||
- | [183] 101.12125 105.53013 105.31742 | ||
- | [190] 117.19709 102.02033 105.12656 114.52400 103.63865 | ||
- | [197] 92.75507 119.69370 | ||
- | [204] 92.09511 112.09925 108.94517 | ||
- | [211] 115.36571 | ||
- | [218] 99.20103 104.54808 112.76252 112.64838 102.69254 | ||
- | [225] 94.85972 | ||
- | [232] 108.01970 | ||
- | [239] 110.56668 | ||
- | [246] 93.40272 | ||
- | [253] 108.19599 | ||
- | [260] 98.52844 | ||
- | [267] 102.02560 | ||
- | [274] 104.00002 | ||
- | [281] 88.26203 | ||
- | [288] 108.46140 | ||
- | [295] 105.24941 106.15145 | ||
- | [302] 97.60201 111.84383 102.07508 | ||
- | [309] 110.61679 | ||
- | [316] 75.33747 | ||
- | [323] 89.50354 100.46007 | ||
- | [330] 104.83076 | ||
- | [337] 105.38950 109.99385 103.41489 | ||
- | [344] 105.41909 109.69636 | ||
- | [351] 96.70728 102.61308 | ||
- | [358] 111.95344 107.72516 | ||
- | [365] 96.64918 107.50832 119.97207 117.91908 | ||
- | [372] 100.60763 | ||
- | [379] 93.20977 104.86308 102.26175 | ||
- | [386] 105.42192 110.65204 | ||
- | [393] 97.33460 102.65806 111.39838 | ||
- | [400] 96.16458 | ||
- | > y <- 1.4*x + 2 + rnorm(400, 0, 4) | ||
- | > y | ||
- | [1] 136.15334 141.36678 125.14346 146.75969 146.91839 155.13529 145.17834 | ||
- | [8] 140.52125 161.16237 135.83550 147.21609 129.83101 159.41017 126.32554 | ||
- | [15] 145.81444 139.03712 132.60136 139.06414 123.22188 109.68975 135.90381 | ||
- | [22] 148.57810 140.43266 127.95524 149.80964 122.10795 148.09642 145.29754 | ||
- | [29] 145.77165 144.31213 150.30475 148.80207 157.55103 114.00139 161.66950 | ||
- | [36] 127.88826 150.29708 151.97808 118.98413 149.13802 150.82833 151.42978 | ||
- | [43] 108.12141 136.10181 125.61718 151.09330 156.38194 130.33149 133.18045 | ||
- | [50] 122.21219 122.85046 136.58148 143.17756 123.49512 146.02445 126.81431 | ||
- | [57] 145.61904 150.21359 156.55289 136.50530 133.85461 120.63638 133.04161 | ||
- | [64] 146.26457 142.13102 142.84121 131.16752 138.52898 133.08298 142.97334 | ||
- | [71] 166.22456 158.22759 162.71390 149.30672 115.86546 126.40807 149.41418 | ||
- | [78] 122.28137 140.04994 139.39608 171.72069 161.62761 141.84821 138.57647 | ||
- | [85] 116.28359 150.62291 137.79468 160.29574 148.09053 168.70778 160.24102 | ||
- | [92] 138.29111 132.96101 128.48436 142.14526 126.44547 135.23103 157.61361 | ||
- | [99] 149.99908 123.61788 140.66349 131.05680 170.68264 156.12262 152.34996 | ||
- | [106] 139.81179 145.36247 109.25937 136.81783 164.65179 138.55190 142.16435 | ||
- | [113] 139.73879 165.17218 153.19408 133.89316 129.36258 128.38218 131.11035 | ||
- | [120] 155.23088 130.18606 132.53642 113.62406 139.44472 154.42732 129.94617 | ||
- | [127] 135.34784 140.77735 174.19736 118.59002 135.46041 143.31440 145.48961 | ||
- | [134] 115.86949 154.57100 113.05077 167.94167 173.29327 158.94914 138.90997 | ||
- | [141] 132.15036 130.16013 123.12985 140.48566 156.88936 145.09040 149.74647 | ||
- | [148] 131.69242 141.19274 132.39223 129.25680 146.18736 133.97415 152.13388 | ||
- | [155] 136.41302 101.31183 155.30386 165.98746 139.15260 143.44305 134.06869 | ||
- | [162] 149.77271 128.09894 119.99632 132.67192 152.28072 130.36562 119.87791 | ||
- | [169] 146.93628 120.59761 145.57902 142.95496 153.13412 155.06478 149.48563 | ||
- | [176] 156.21369 142.86568 142.69522 156.10727 130.39935 152.61788 123.07734 | ||
- | [183] 142.67060 150.94834 150.14259 127.43596 138.46810 142.98959 141.95404 | ||
- | [190] 161.79946 144.36598 151.94688 155.64581 147.73510 126.63936 142.25827 | ||
- | [197] 127.70118 170.28144 141.24582 146.34713 138.14630 118.34356 146.34777 | ||
- | [204] 127.55320 156.31519 144.49527 141.25458 151.02216 145.78275 146.27848 | ||
- | [211] 163.49551 112.91190 151.03768 139.75291 155.02415 156.16073 143.62611 | ||
- | [218] 137.50858 154.10782 155.95803 160.84298 147.02123 146.39854 149.88526 | ||
- | [225] 135.00785 134.35517 153.71808 139.42892 146.75898 131.42651 151.92026 | ||
- | [232] 152.63363 131.94064 156.00171 152.01589 140.00259 142.24212 135.06532 | ||
- | [239] 156.13705 139.03987 149.63894 138.52558 105.47473 144.58488 143.57210 | ||
- | [246] 131.25376 120.69584 138.64322 147.25182 130.93258 136.01900 132.83907 | ||
- | [253] 147.34248 138.79248 126.84719 120.63842 140.19360 148.00099 128.20340 | ||
- | [260] 141.75121 134.64641 126.47734 163.93086 135.37476 177.71675 151.46994 | ||
- | [267] 143.59227 144.39371 149.66929 127.55865 152.88301 141.01573 135.69735 | ||
- | [274] 153.18730 121.72656 145.10166 145.46457 127.04068 139.68628 121.98121 | ||
- | [281] 127.61569 108.13119 149.86375 155.90616 158.57453 120.39602 138.54738 | ||
- | [288] 147.72705 126.09845 141.08382 136.63450 162.44393 140.00548 145.29970 | ||
- | [295] 145.84471 145.87345 134.98375 153.15825 138.22498 141.74321 125.78910 | ||
- | [302] 139.22164 157.63110 141.93373 142.43277 130.12262 156.29907 130.35248 | ||
- | [309] 153.92230 136.93778 142.08328 139.87360 120.68367 151.90523 160.57232 | ||
- | [316] 108.41862 139.70771 175.21783 135.67468 156.81052 145.72460 130.22365 | ||
- | [323] 126.24873 140.64648 131.17491 130.68261 141.79391 137.54841 108.48632 | ||
- | [330] 147.75584 145.52284 135.52419 155.60041 137.11896 131.39066 159.55025 | ||
- | [337] 157.32631 150.35471 146.15982 125.19119 145.43292 121.85669 129.99533 | ||
- | [344] 150.54293 156.22357 140.02952 151.83912 130.16063 131.44195 162.99746 | ||
- | [351] 135.54709 146.14491 132.91331 156.74646 143.75338 108.51161 164.03761 | ||
- | [358] 157.74286 150.92070 117.53851 131.10239 141.45090 158.11971 130.93894 | ||
- | [365] 140.37843 154.53988 169.47647 170.81682 118.81946 | ||
- | [372] 144.66285 138.32993 141.59815 153.62912 149.58162 153.25996 123.22005 | ||
- | [379] 129.33102 148.02073 147.81094 120.44112 135.01371 143.00462 185.69520 | ||
- | [386] 143.88558 159.06145 131.69241 152.04899 125.41866 143.80242 158.80394 | ||
- | [393] 138.00040 144.97010 157.22821 127.05684 131.59783 110.14579 133.96685 | ||
- | [400] 138.35520 | ||
- | > 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 = " | ||
- | Warning message: | ||
- | In geom_point(color = " | ||
- | Ignoring unknown parameters: `siz` | ||
- | > | ||
- | > | ||
> set.seed(401) | > set.seed(401) | ||
> sn <- 25 | > sn <- 25 | ||
> x <- rnorm(sn, 100, 10) | > x <- rnorm(sn, 100, 10) | ||
> x | > x | ||
- | | + | |
- | | + | [11] |
- | [15] 103.18120 101.21572 115.59812 104.79399 | + | [21] 114.26808 |
- | [22] 113.21215 110.42156 104.10994 107.89136 | + | |
> y <- 1.4 * x + 2 + rnorm(sn, 0, 10) | > y <- 1.4 * x + 2 + rnorm(sn, 0, 10) | ||
> y | > y | ||
- | [1] 147.7866 178.1177 167.8750 124.8276 147.9924 133.5853 144.6882 102.0537 | + | [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 |
- | [17] 161.4669 147.8364 127.3562 122.0032 168.4221 138.2663 147.7574 135.0859 | + | [23] 147.7574 135.0859 153.9057 |
- | [25] 153.9057 | + | |
> df <- data.frame(x, | > df <- data.frame(x, | ||
> # density graph | > # density graph | ||
> ggplot(data=df, | > ggplot(data=df, | ||
- | + | + | + |
- | + | + | + |
- | + color=" | + | + color=" |
- | + | + | + |
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`. | `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. | ||
> | > | ||
> ggplot(data=df, | > ggplot(data=df, | ||
- | + | + | + |
- | + | + | + |
- | + color=" | + | + color=" |
- | + | + | + |
> | > | ||
> lm.mod <- lm(y~x, data=df) | > lm.mod <- lm(y~x, data=df) | ||
Line 333: | Line 252: | ||
> | > | ||
> ggplot(data=df, | > ggplot(data=df, | ||
- | + | + | + |
- | + | + | + |
- | + | + | + |
> | > | ||
> | > | ||
> 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.1683476124.txt.gz · Last modified: 2023/05/08 01:15 by hkimscil