User Tools

Site Tools


c:ms:2023:schedule:w10.lecture.note

Table of Contents

R code

set.seed(401)
sn <- 25
x <- rnorm(sn, 100, 10)
x
y <- 1.4 * x + 2 + rnorm(sn, 0, 10)
y
df <- data.frame(x, y)
# density graph
ggplot(data=df, aes(y)) + 
  geom_histogram() + 
  geom_vline(aes(xintercept=mean(y)),
             color="red", linetype="dashed", size=1) +
  coord_flip()

ggplot(data=df, aes(y)) + 
  geom_density(color="blue", size=1.5) +
  geom_vline(aes(xintercept=mean(y)),
             color="red", linetype="dashed", size=1) +
  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, aes(x,y)) +
  geom_point(color="blue", size=1.5, pch=1.5) +
  geom_hline(aes(yintercept=mean(y))) +
  geom_abline(intercept=inc.y, slope=slope.x)


ggplot(data=df, aes(x,y)) +
  geom_point(color="blue", size=2.5, pch=2) +
  geom_hline(aes(yintercept=mean(y)), size=1.5, color="red") +
  geom_abline(intercept=inc.y, slope=slope.x, size=1.5, color="darkgreen")

################################
################################
################################
################################

set.seed(101)
sn <- 400
x <- rnorm(sn, 100, 10)
x
y <- 1.4*x + 2 + rnorm(sn, 0, 16)
y
df <- data.frame(x,y)
# density graph
ggplot(data=df, aes(y)) + 
  geom_histogram() + 
  geom_vline(aes(xintercept=mean(y)),
             color="red", linetype="dashed", size=1) +
  coord_flip()

ggplot(data=df, aes(y)) + 
  geom_density(color="blue", size=1.5) +
  geom_vline(aes(xintercept=mean(y)),
             color="red", linetype="dashed", size=1) +
  coord_flip()

ggplot(data=df, aes(x,y)) +
  geom_point(color="blue", size=1.5, pch=1.5) +
  geom_hline(aes(yintercept=mean(y)), size=1, color="darkgreen") +
  stat_smooth(method = "lm",
              formula = y ~ x,
              geom = "smooth", color="red", size=1)



ggplot(data=df, aes(x,y)) +
  geom_point(color="blue", size=1.5, pch=2) +
  geom_hline(aes(yintercept=mean(y)), size=1, color="darkgreen") +
  geom_abline(intercept=10, slope=1.5, size=1.5, color="red")

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]

ggplot(data=df, aes(x,y)) +
  geom_point(color="blue", size=1.5, pch=2) +
  geom_hline(aes(yintercept=mean(y)), size=1, color="darkgreen") +
  geom_abline(intercept=a, slope=b, size=1.5, color="red")

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.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
 [1]  99.04030 112.53423 111.25717  95.37048 106.51630 110.03586  99.37429  83.40702  91.38017  80.14344
[11]  95.16165 105.55799 100.47560  95.35164 103.18120 101.21572 115.59812 104.79399  89.67882  86.01922
[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, y)
> # density graph
> ggplot(data=df, aes(y)) + 
+   geom_histogram() + 
+   geom_vline(aes(xintercept=mean(y)),
+              color="red", linetype="dashed", size=1) +
+   coord_flip()
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
> 
> ggplot(data=df, aes(y)) + 
+   geom_density(color="blue", size=1.5) +
+   geom_vline(aes(xintercept=mean(y)),
+              color="red", linetype="dashed", size=1) +
+   coord_flip()
> 
> lm.mod <- lm(y~x, data=df)
> summary(lm.mod)

Call:
lm(formula = y ~ x, data = df)

Residuals:
    Min      1Q  Median      3Q     Max 
-19.958  -6.345  -0.137   6.596  20.954 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -5.9221    22.5365  -0.263    0.795    
x             1.4492     0.2212   6.553  1.1e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 10.75 on 23 degrees of freedom
Multiple R-squared:  0.6512,	Adjusted R-squared:  0.636 
F-statistic: 42.94 on 1 and 23 DF,  p-value: 1.097e-06

> str(lm.mod)
List of 12
 $ coefficients : Named num [1:2] -5.92 1.45
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
 $ residuals    : Named num [1:25] 10.18 20.95 12.56 -7.46 -0.45 ...
  ..- attr(*, "names")= chr [1:25] "1" "2" "3" "4" ...
 $ effects      : Named num [1:25] -705.43 -70.46 7.59 -7.34 -3.9 ...
  ..- attr(*, "names")= chr [1:25] "(Intercept)" "x" "" "" ...
 $ rank         : int 2
 $ fitted.values: Named num [1:25] 138 157 155 132 148 ...
  ..- attr(*, "names")= chr [1:25] "1" "2" "3" "4" ...
 $ assign       : int [1:2] 0 1
 $ 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(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:25] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:2] "(Intercept)" "x"
  .. ..- attr(*, "assign")= int [1:2] 0 1
  ..$ qraux: num [1:2] 1.2 1.24
  ..$ pivot: int [1:2] 1 2
  ..$ tol  : num 1e-07
  ..$ rank : int 2
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 23
 $ xlevels      : Named list()
 $ call         : language lm(formula = y ~ x, data = df)
 $ terms        :Classes 'terms', 'formula'  language y ~ x
  .. ..- attr(*, "variables")= language list(y, x)
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "y" "x"
  .. .. .. ..$ : chr "x"
  .. ..- attr(*, "term.labels")= chr "x"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(y, x)
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
 $ model        :'data.frame':	25 obs. of  2 variables:
  ..$ y: num [1:25] 148 178 168 125 148 ...
  ..$ x: num [1:25] 99 112.5 111.3 95.4 106.5 ...
  ..- attr(*, "terms")=Classes 'terms', 'formula'  language y ~ x
  .. .. ..- attr(*, "variables")= language list(y, x)
  .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:2] "y" "x"
  .. .. .. .. ..$ : chr "x"
  .. .. ..- attr(*, "term.labels")= chr "x"
  .. .. ..- attr(*, "order")= int 1
  .. .. ..- attr(*, "intercept")= int 1
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. ..- attr(*, "predvars")= language list(y, x)
  .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
 - attr(*, "class")= chr "lm"
> inc.y <- lm.mod$coefficients[1]
> slope.x <- lm.mod$coefficients[2]
> inc.y
(Intercept) 
   -5.92206 
> slope.x
       x 
1.449211 
> 
> ggplot(data=df, aes(x,y)) +
+   geom_point(color="blue", size=1.5, pch=1.5) +
+   geom_hline(aes(yintercept=mean(y))) +
+   geom_abline(intercept=inc.y, slope=slope.x)
> 
> 
> ggplot(data=df, aes(x,y)) +
+   geom_point(color="blue", size=2.5, pch=2) +
+   geom_hline(aes(yintercept=mean(y)), size=1.5, color="red") +
+   geom_abline(intercept=inc.y, slope=slope.x, size=1.5, color="darkgreen")
> 
> ################################
> ################################
> ################################
> ################################
> 
> set.seed(101)
> sn <- 400
> x <- rnorm(sn, 100, 10)
> x
  [1]  96.73964 105.52462  93.25056 102.14359 103.10769 111.73966 106.18790  98.87266 109.17028  97.76741
 [11] 105.26448  92.05156 114.27756  85.33180  97.63317  98.06662  91.50245 100.58465  91.82330  79.49692
 [21]  98.36244 107.08522  97.32019  85.36078 107.44436  85.89610 104.67068  98.80680 104.67239 104.98136
 [31] 108.94937 102.79152 110.07866  79.26894 111.89853  92.75626 101.67984 109.20335  83.28395 104.48469
 [41] 104.82459 107.58214  76.80673  95.40495  88.94616 104.02928 105.68935  92.93917  97.09909  85.16122
 [51]  88.49745  97.25529 105.77901  86.03097 107.49058  89.48813 101.65381 111.29809 111.73722  95.72137
 [61]  97.40198  85.88827  93.58642 101.12458 104.22604 103.86835  93.12202 101.48902  99.42350  99.25177
 [71] 115.09897 116.19937 111.53158  99.22396  81.81065  89.62555 103.02492  87.22054 101.38339  99.49016
 [81] 118.52148 111.11675  94.88625  94.56119  82.71073 104.70750 100.05387 113.48046 107.24097 115.52549
 [91] 113.25470  99.65735  96.38987  92.79835 102.82015  92.09474  95.55095 113.64993 104.97454  91.85604
[101] 102.68066  94.07792 121.33486 111.72749 107.46761  97.69491 100.87772  78.16260  95.33368 116.85960
[111]  94.32079  99.53257  98.43019 116.02242 107.68654  92.28371  93.69318  91.69719  94.08887 109.81085
[121]  93.38395  92.27582  79.81527  94.66415 104.34728  92.28833  92.46059  97.00642 116.63966  87.55670
[131]  92.16866 102.44831  98.56113  83.91369 109.51580  81.80868 117.83672 118.87139 114.90719  96.19400
[141]  90.90625  96.61906  85.88116 102.17543 106.70126  97.12141 104.69303  95.29929  97.60734  95.52538
[151]  93.81170 102.52963  92.46632 107.32277  95.97413  71.77000 104.62974 121.32870  97.29513 102.48525
[161] 100.38116 103.94069  84.95915  84.13109  90.72882 107.76197  92.19316  87.21433  99.98572  81.49022
[171] 104.51505  95.67053 107.13603 109.60695 103.81535 112.18073  99.82863  99.61791 112.43734  90.44141
[181] 109.15425  90.60662 101.12125 105.53013 105.31742  91.26238  98.13151  97.86290  97.95989 117.19709
[191] 102.02033 105.12656 114.52400 103.63865  91.24151  99.85439  92.75507 119.69370  94.63598  99.73768
[201]  98.35968  86.16725 104.23511  92.09511 112.09925 108.94517  98.98801 102.97123 101.97298  98.43016
[211] 115.36571  78.32330 105.98448 100.43112 112.95027 107.06303 103.45545  99.20103 104.54808 112.76252
[221] 112.64838 102.69254  98.79456 107.95271  94.85972  95.93407 112.19719 100.83711 105.89902  94.82581
[231] 107.69463 108.01970  93.03140 111.77853 105.85845  95.33106 103.85650  94.65394 110.56668  97.93907
[241] 106.07012  94.51936  79.00024 102.50813  99.45055  93.40272  85.44143 100.23729 105.47908  91.91099
[251]  97.60977  96.46881 108.19599  96.54720  88.17116  89.68679  99.24911 108.28585  89.64015  98.52844
[261]  97.18546  86.26314 115.58450  94.25055 121.87335 107.94292 102.02560  99.77950 103.04531  88.90751
[271] 107.65473  99.77974  90.96002 104.00002  88.50712 101.88168 102.17290  89.50914  99.24877  84.32011
[281]  88.26203  78.51694 103.41715 109.04971 110.96490  85.28739  97.19403 108.46140  87.14086  96.87564
[291]  96.37157 114.12454  97.44798 103.87018 105.24941 106.15145  96.67184 107.45329  96.88735  97.65749
[301]  88.34143  97.60201 111.84383 102.07508  99.56367  90.28922 105.74223  87.40135 110.61679  95.66006
[311] 102.39177  94.97671  82.96123 105.23772 115.64172  75.33747  98.51179 121.10837  96.53805 109.82183
[321] 103.91431  94.90006  89.50354 100.46007  89.91172  93.43750 100.94018 100.17924  83.96476 104.83076
[331]  97.75577  95.62602 113.27768  95.31522  86.60042 116.65433 105.38950 109.99385 103.41489  89.41373
[341]  99.58401  87.07172  91.06136 105.41909 109.69636  97.55011 108.60180  93.24156  94.99956 118.32033
[351]  96.70728 102.61308  88.87839 110.62210 102.05000  79.75672 112.27375 111.95344 107.72516  85.90462
[361]  95.67570  95.57712 113.48992  89.25368  96.64918 107.50832 119.97207 117.91908  87.67024  68.22790
[371]  89.15158 100.60763  98.18234 103.41311 111.06792 106.88816 104.70992  83.59933  93.20977 104.86308
[381] 102.26175  93.53319  98.86714 105.20524 125.86743 105.42192 110.65204  92.49852 107.93791  92.50237
[391] 103.06302 113.18370  97.33460 102.65806 111.39838  90.13455  93.26333  80.95256  92.82078  96.16458
> 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  98.87192
 [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  97.81333 175.77054 148.36930 141.97085 137.33414
[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  84.09791 141.80539 150.59611 126.72361 117.92938 127.57624 139.99515 131.70418
[251] 128.11498 120.18725 128.94675 143.67165 131.06991  99.86916 137.92815 131.20339 130.32497 147.18538
[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  96.75362 159.10296 159.61588 162.24551 117.37702 139.97459 129.37033 132.40218 151.45761
[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  75.29330 144.73415
[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  93.06820 178.60071 154.76698 145.23716 103.35464
[361] 116.57163 158.37971 149.82117 142.89029 149.58717 160.62458 168.02319 182.00714 101.06283  75.58043
[371] 133.84178 150.09934 134.95391 126.05753 142.03125 143.39621 167.25819 135.76302 119.84304 145.65798
[381] 155.74441  82.92507 118.81283 124.15648 208.13760 126.77023 165.50725 132.27584 148.85673 107.16469
[391] 136.34497 153.84423 137.19628 142.71654 155.03963 123.66223 128.68534  94.58241 140.02013 143.52958
> df <- data.frame(x,y)
> # density graph
> ggplot(data=df, aes(y)) + 
+   geom_histogram() + 
+   geom_vline(aes(xintercept=mean(y)),
+              color="red", linetype="dashed", size=1) +
+   coord_flip()
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
> 
> ggplot(data=df, aes(y)) + 
+   geom_density(color="blue", size=1.5) +
+   geom_vline(aes(xintercept=mean(y)),
+              color="red", linetype="dashed", size=1) +
+   coord_flip()
> 
> ggplot(data=df, aes(x,y)) +
+   geom_point(color="blue", size=1.5, pch=1.5) +
+   geom_hline(aes(yintercept=mean(y)), size=1, color="darkgreen") +
+   stat_smooth(method = "lm",
+               formula = y ~ x,
+               geom = "smooth", color="red", size=1)
> 
> 
> 
> ggplot(data=df, aes(x,y)) +
+   geom_point(color="blue", size=1.5, pch=2) +
+   geom_hline(aes(yintercept=mean(y)), size=1, color="darkgreen") +
+   geom_abline(intercept=10, slope=1.5, size=1.5, color="red")
> 
> 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      3Q     Max 
-48.386 -10.834  -0.276   8.934  37.376 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.90168    7.62463   0.118    0.906    
x            1.39426    0.07609  18.324   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 14.66 on 398 degrees of freedom
Multiple R-squared:  0.4576,	Adjusted R-squared:  0.4562 
F-statistic: 335.8 on 1 and 398 DF,  p-value: < 2.2e-16

> 
> lm.mod2$coefficients[2]
       x 
1.394256 
> lm.mod2$coefficients[1]
(Intercept) 
  0.9016802 
> 
> b <- lm.mod2$coefficients[2]
> a <- lm.mod2$coefficients[1]
> 
> ggplot(data=df, aes(x,y)) +
+   geom_point(color="blue", size=1.5, pch=2) +
+   geom_hline(aes(yintercept=mean(y)), size=1, color="darkgreen") +
+   geom_abline(intercept=a, slope=b, size=1.5, color="red")
> 
> lm.mod2$residuals
          1           2           3           4           5           6           7           8           9 
 -3.4746174 -31.7662994 -27.9953774   8.7196574   3.9610280 -11.4608149 -20.2306343   2.0643286  27.0212529 
         10          11          12          13          14          15          16          17          18 
-10.4955793  -6.9137777  -2.5376141  -8.5589127  21.0324963  30.1711479   0.6369969  11.6155907 -13.3414636 
         19          20          21          22          23          24          25          26          27 
-27.6971994 -12.8688367 -13.5511384 -11.6514560  10.3948753  27.3891900  -8.7343750   1.0053441  -0.0705950 
         28          29          30          31          32          33          34          35          36 
 21.5379341  -9.3792404 -16.9457691 -15.1733880  13.2644964   7.4942308   5.6531643  13.7872501 -14.2508958 
         37          38          39          40          41          42          43          44          45 
 25.4635703  -9.9008955   3.1230920   5.1362849   9.9960200  -3.0245953  -4.0925309   3.7858216  -2.0205986 
         46          47          48          49          50          51          52          53          54 
 15.5050718  27.3727744  -5.5012269 -17.3771050   5.5334144 -10.5772242  -4.6467711 -25.9463125   5.7994998 
         55          56          57          58          59          60          61          62          63 
-24.1337116  -0.2639915   6.8970486 -28.6773575  -5.7767647   3.6296544 -16.3748583  -4.8371426   1.7183225 
         64          65          66          67          68          69          70          71          72 
 12.4398255 -21.4447985 -16.6030004  -3.1800045 -20.5413749 -30.7702866   9.7518575  14.1034204 -24.0403803 
         73          74          75          76          77          78          79          80          81 
 20.0176731  35.2409359  -1.1095846  -2.6577263  14.4072229  -5.7102332 -13.8665975  -5.8907808  16.9415652 
         82          83          84          85          86          87          88          89          90 
 17.9931867  29.6732031  18.4046991  -4.4723224   9.8294162 -15.4499433  -0.5574663 -14.4729952  21.6502481 
         91          92          93          94          95          96          97          98          99 
  0.4866024 -11.2459893 -14.2872615 -12.1019636 -13.5229133 -16.3214061  -0.5140732 -12.2340766   5.8401374 
        100         101         102         103         104         105         106         107         108 
-26.2963375 -18.6696323  -8.9704600  -2.9494460  -7.4434017   1.2968079   5.8150963  10.2124233  -7.1258268 
        109         110         111         112         113         114         115         116         117 
  7.0485934  -2.0370668  19.6512446   4.9450222   1.4097695   4.7278671   3.4485490  12.4122630 -13.5949983 
        118         119         120         121         122         123         124         125         126 
 -6.3505617  -8.8175514  -0.2882130  -8.5711641   7.0294129   1.0874987  21.3017029  27.0621633  -3.4015577 
        127         128         129         130         131         132         133         134         135 
 17.2414383  13.5289414  37.3755979 -22.3562059  19.3248889  -6.7661424  23.6805735 -12.8583893  -1.2771132 
        136         137         138         139         140         141         142         143         144 
-12.3573364   5.6562192  21.2743769 -13.9253690  10.6043118  13.1469240 -26.7729251   5.1765012 -16.5545580 
        145         146         147         148         149         150         151         152         153 
 23.7415811  30.1379044   6.4045552 -13.2606073  11.8288094 -11.7261952 -14.6812000   4.2707380  11.7146435 
        154         155         156         157         158         159         160         161         162 
  1.2427601   1.8465318  -3.1541202  28.9882028 -21.6956671   5.4148290  -6.4582415 -32.1848782  10.7183154 
        163         164         165         166         167         168         169         170         171 
 30.2108509   2.4327253  16.2257323  -0.6268704  -1.1913196 -15.2893256  21.4977219  19.6116057  -9.2695699 
        172         173         174         175         176         177         178         179         180 
 29.7127197   6.2883910   0.1880614  10.2711793  -9.6146350   6.0941300   6.5911259 -11.4759098   8.7432830 
        181         182         183         184         185         186         187         188         189 
 -7.0670145 -21.4689644  -1.9174567   6.5291240   4.4960517  -7.7029489  -2.0020893  17.5865667  12.9017805 
        190         191         192         193         194         195         196         197         198 
-15.3344050  -0.1656470  12.7809341 -24.9950438   4.2575262 -10.7726382   3.5203550 -14.9925564   4.6268558 
        199         200         201         202         203         204         205         206         207 
 28.6637087  20.5287197  -4.5657238 -15.5691273  -4.6285154 -11.8925051  -8.7528513 -38.3877830   4.3523274 
        208         209         210         211         212         213         214         215         216 
 21.1395345   5.7663682  27.5686900   1.6950174   6.5853138   4.3446907  -9.7274554 -18.6778369  18.8032432 
        217         218         219         220         221         222         223         224         225 
-11.1535346 -11.8233452  24.6608838 -13.8920218   6.2863442   6.6948766  26.0104146 -11.2757601   2.4601369 
        226         227         228         229         230         231         232         233         234 
 -6.1607243 -19.6891885 -13.2946477 -12.2920126 -11.6754864  -1.6920089  -0.6570117   0.4193871  -8.2125793 
        235         236         237         238         239         240         241         242         243 
  8.9625719  19.8022864 -18.9330501   3.8411704  -0.8918261   1.3615594  -1.7293776  18.4351146 -26.9503418 
        244         245         246         247         248         249         250         251         252 
 -2.0188815  11.0348899  -4.4053943  -2.0995328 -13.0819055  -7.9713881   2.6550375  -8.8797268 -15.2166647 
        253         254         255         256         257         258         259         260         261 
-22.8078616   8.1584308   7.2350489 -26.0788823  -1.3522121 -20.6765143   4.4419549   8.9098171 -11.9963947 
        262         263         264         265         266         267         268         269         270 
 16.4295674   2.2124356   7.3356495  22.1745595  -4.8822450  -3.2899706  12.4810919  15.3136167   5.9615531 
        271         272         273         274         275         276         277         278         279 
  2.3822204  -1.0321771  27.0340866  24.0447723 -15.1269755   3.5527724   3.3752225   0.5239755  -3.3795901 
        280         281         282         283         284         285         286         287         288 
  9.3148527   9.8006871 -13.6207847  14.0112681   6.6709718   6.6303285  -2.4371321   3.5595267 -22.7543261 
        289         290         291         292         293         294         295         296         297 
 10.0038149  15.4864625   0.5090688   4.4321302   7.9712729  -6.7792422 -12.3149711 -17.2463007  -7.7736970 
        298         299         300         301         302         303         304         305         306 
  4.6101104   3.9856124  13.7501494   2.0501165   3.9742149  -2.0603007 -10.2009494   5.8446841   8.4877582 
        307         308         309         310         311         312         313         314         315 
 26.7454828  25.5627249 -10.0311236   5.7025416 -11.3743320  21.2686430  11.7266151  11.9924647 -11.5418205 
        316         317         318         319         320         321         322         323         324 
  5.3156858   0.8289472  16.4584012  -4.2615220   5.9689829  -5.3265662 -16.9023231  -2.6124888  -6.3151418 
        325         326         327         328         329         330         331         332         333 
 14.8087721  -6.8845717  -4.4112764 -17.1363971 -42.6767643  -2.3284644  28.3188400   0.2386305 -18.2044305 
        334         335         336         337         338         339         340         341         342 
  8.3563734  34.1960270 -21.2948682  32.8277064 -20.8166195  -0.7918255  -6.3402167  17.7315363  -6.5764400 
        343         344         345         346         347         348         349         350         351 
  3.6590539   5.5286408   4.3230679   7.4960704  -7.0915009  -7.8763515 -12.5857940 -16.8260691  -5.7185980 
        352         353         354         355         356         357         358         359         360 
  3.6341103  27.5430616   1.2357534  -2.7820224 -19.0347891  21.1606580  -2.2264760  -5.8609865 -17.3200922 
        361         362         363         364         365         366         367         368         369 
-17.7264836  24.2190429  -9.3145369  17.5461131  13.9317778   9.8287519  -0.1502889  16.6960559 -22.0736253 
        370         371         372         373         374         375         376         377         378 
-20.4484231   8.6399497   8.9248462  -2.8391012 -19.0285217 -13.7275614  -6.5349466  20.3640549  18.3024546 
        379         380         381         382         383         384         385         386         387 
-11.0169399  -1.4497000  12.2636443 -48.3858395 -19.9349754 -23.4282517  31.7444754 -21.1166158  10.3282794 
        388         389         390         391         392         393         394         395         396 
  2.4075270  -2.5380510 -22.7089945  -8.2529627  -4.8645207   0.5852366  -1.3167768  -1.1799373  -2.9101056 
        397         398         399         400 
 -2.2493160 -19.1878786   9.7025061   8.5498421 
> 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             2             3             4             5             6             7 
 -4.188193540   8.060324036  -9.052855916   3.346310443   4.690509466  16.725688058   8.985106285 
            8             9            10            11            12            13            14 
 -1.214214749  13.143314113  -2.755217099   7.697625625 -10.724577336  20.264160488 -20.093633982 
           15            16            17            18            19            20            21 
 -2.942382245  -2.338036133 -11.490167661   1.172749237 -11.042829145 -28.228953240  -1.925583097 
           22            23            24            25            26            27            28 
 10.236203716  -3.378744942 -20.053229354  10.736932962 -19.306862002   6.869709427  -1.306037575 
           29            30            31            32            33            34            35 
  6.872098565   7.302876361  12.835307738   4.249684406  14.409821034 -28.546825176  16.947194881 
           36            37            38            39            40            41            42 
 -9.742041972   2.699714560  13.189420340 -22.948863166   6.610398182   7.084302239  10.929033016 
           43            44            45            46            47            48            49 
-31.979775017  -6.049083432 -15.054289797   5.975442811   8.290000729  -9.487019465  -3.687016096 
           50            51            52            53            54            55            56 
-20.331471466 -15.679915057  -3.469240752   8.415010924 -19.118811218  10.801373996 -14.298645187 
           57            58            59            60            61            62            63 
  2.663423441  16.110024023  16.722288536  -5.607919185  -3.264716559 -19.317777113  -8.584576990 
           64            65            66            67            68            69            70 
  1.925536197   6.249777464   5.751065411  -9.232080329   2.433672581  -0.446194773  -0.685638984 
           71            72            73            74            75            76            77 
 21.409428922  22.943662437  16.435569549  -0.724402520 -25.003016473 -14.107044909   4.575107276 
           78            79            80            81            82            83            84 
-17.460253140   2.286391149  -0.353258888  26.181272643  15.857191683  -6.772291669  -7.225505548 
           85            86            87            88            89            90            91 
-23.748085276   6.921045005   0.432700698  19.152802236  10.453353668  22.004103260  18.838035602 
           92            93            94            95            96            97            98 
 -0.120152760  -4.675861233  -9.683360572   4.289601082 -10.664362592  -5.845518829  19.389092184 
           99           100           101           102           103           104           105 
  7.293378323 -10.997182870   4.095114946  -7.899310626  30.103856034  16.708711308  10.769351738 
          106           107           108           109           110           111           112 
 -2.856291313   1.581352807 -30.089335260  -6.148449365  23.864189832  -7.560682256  -0.294126975 
          113           114           115           116           117           118           119 
 -1.831121148  22.696954827  11.074591845 -10.400899658  -8.435732181 -11.218648193  -7.884035535 
          120           121           122           123           124           125           126 
 14.036434436  -8.866881966 -10.411890997 -27.785100861  -7.081957406   6.418817120 -10.394457998 
          127           128           129           130           131           132           133 
-10.154276177  -3.816224498  23.557545329 -16.991555363 -10.561309080   3.771155620  -1.648565341 
          134           135           136           137           138           139           140 
-22.070852707  13.625053083 -25.005765812  25.226543589  26.669147778  21.142029261  -4.948941984 
          141           142           143           144           145           146           147 
-12.321426981  -4.356307611 -19.327682961   3.390695591   9.700866033  -3.655906788   6.900883453 
          148           149           150           151           152           153           154 
 -6.196409564  -2.978389475  -5.881183034  -8.270480396   3.884543420 -10.146292007  10.567405765 
          155           156           157           158           159           160           161 
 -5.255499753 -39.002263512   6.812631665  30.095258615  -3.413686972   3.822670480   0.889031722 
          162           163           164           165           166           167           168 
  5.851921144 -20.613210572 -21.767732731 -12.568810769  11.179765694 -10.527150723 -17.468909475 
          169           170           171           172           173           174           175 
  0.337678648 -25.449786671   6.652731499  -5.678800699  10.307043017  13.752146484   5.677168696 
          176           177           178           179           180           181           182 
 17.340645806   0.118653098  -0.175147804  17.698434213 -12.969529318  13.120963429 -12.739187464 
          183           184           185           186           187           188           189 
  1.920897652   8.068003086   7.771435656 -11.824895795  -2.247567141  -2.622081296  -2.486849390 
          190           191           192           193           194           195           196 
 24.334742967   3.174454748   7.505325330  20.607767499   5.430807189 -11.853987746   0.154576491 
          197           198           199           200           201           202           203 
 -9.743700390  27.815654880  -7.121233632  -0.008155611  -1.929440828 -18.928807803   6.262422434 
          204           205           206           207           208           209           210 
-10.663850175  17.227042523  12.829445847  -1.053376512   4.500240233   3.108426135  -1.831165164 
          211           212           213           214           215           216           217 
 21.781326872 -29.865278310   8.701490814   0.958687155  18.413587198  10.205262822   5.175374020 
          218           219           220           221           222           223           224 
 -0.756373622   6.698772801  18.151818084  17.992667603   4.111674430  -1.323103072  11.445710456 
          225           226           227           228           229           230           231 
 -6.809271836  -5.311364177  17.363597791   1.524741376   8.582337678  -6.856559994  11.085882713 
          232           233           234           235           236           237           238 
 11.539103178  -9.358425291  16.779881273   8.525774195  -6.152106461   5.734534353  -7.096181047 
          239           240           241           242           243           244           245 
 15.090255018  -2.515877757   8.820896097  -7.283823851 -28.921459521   3.854562893  -0.408487563 
          246           247           248           249           250           251           252 
 -8.840705400 -19.940791225   0.688439411   7.996832897 -10.920567370  -2.975005986  -4.565789371 
          253           254           255           256           257           258           259 
 11.784903101  -4.456491985 -16.134849344 -14.021667702  -0.689347013  11.910194696 -14.086689781 
          260           261           262           263           264           265           266 
 -1.694142756  -3.566597677 -18.795107076  22.086377996  -7.658616347  30.854647422  11.432050544 
          267           268           269           270           271           272           273 
  3.181799384   0.050155739   4.603531162 -15.108185808  11.030246473   0.050485562 -12.246460775 
          274           275           276           277           278           279           280 
  5.934639390 -15.666422989   2.981129235   3.387171035 -14.269352250  -0.689822699 -21.504192760 
          281           282           283           284           285           286           287 
-16.008148268 -29.595299776   5.121979266  12.975202346  15.645476443 -20.155556179  -3.554648742 
          288           289           290           291           292           293           294 
 12.154944408 -17.571341210  -3.998566035  -4.701373931  20.050816630  -3.200583582   5.753606266 
          295           296           297           298           299           300           301 
  7.676609253   8.934290166  -4.282721357  10.749382792  -3.982246646  -2.908474925 -15.897442211 
          302           303           304           305           306           307           308 
 -2.985817392  16.870919093   3.250788947  -0.250759772 -13.181717777   8.363724287 -17.208161234 
          309           310           311           312           313           314           315 
 15.160112909  -5.693393330   3.692329779  -6.646157354 -23.398814345   7.660317317  22.166157510 
          316           317           318           319           320           321           322 
-34.028292908  -1.717352317  29.788066013  -4.469258871  14.051731373   5.815147000  -6.753033299 
          323           324           325           326           327           328           329 
-14.277169928   0.999047979 -13.708058852  -8.792211285   1.668441234   0.607500727 -21.999649132 
          330           331           332           333           334           335           336 
  7.092908553  -2.771438717  -5.740856271  18.870084347  -6.174191334 -18.324853141  23.577989327 
          337           338           339           340           341           342           343 
  7.871933488  14.291574349   5.118826524 -14.402386692  -0.222410421 -17.667739509 -12.105163697 
          344           345           346           347           348           349           350 
  7.913192957  13.876800158  -3.058182742  12.350704495  -9.065405731  -6.614299669  25.900822413 
          351           352           353           354           355           356           357 
 -4.233308503   4.000891801 -15.148776649  15.167525530   3.215819010 -27.866724081  17.470338913 
          358           359           360           361           362           363           364 
 17.023749956  11.128436252 -19.294980783  -5.671591247  -5.809038961  19.165998537 -14.625527113 
          365           366           367           368           369           370           371 
 -4.314316955  10.826117811  28.203766718  25.341376354 -16.833258109 -43.940853091 -14.767882592 
          372           373           374           375           376           377           378 
  1.204788347  -2.176695408   5.116340351  15.789100676   9.961452371   6.924422052 -22.509145717 
          379           380           381           382           383           384           385 
 -9.109732108   7.137970613   3.511054869  -8.658795795  -1.221903897   7.615023765  36.423415403 
          386           387           388           389           390           391           392 
  7.917140730  15.209261430 -10.101392648  11.425071990 -10.096024661   4.628228753  18.739043938 
          393           394           395           396           397           398           399 
 -3.358662973   4.063609447  16.249858053 -13.397371823  -9.035050482 -26.199424528  -9.652082743 
          400 
 -4.989967840 
> 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      3Q     Max 
-48.386 -10.834  -0.276   8.934  37.376 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.90168    7.62463   0.118    0.906    
x            1.39426    0.07609  18.324   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 14.66 on 398 degrees of freedom
Multiple R-squared:  0.4576,	Adjusted R-squared:  0.4562 
F-statistic: 335.8 on 1 and 398 DF,  p-value: < 2.2e-16

> 
> 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.txt · Last modified: 2023/05/08 01:56 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki