c:ma:regression_lecture_note
Differences
This shows you the differences between two versions of the page.
| c:ma:regression_lecture_note [2023/10/27 22:18] – created hkimscil | c:ma:regression_lecture_note [2023/10/27 22:19] (current) – hkimscil | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| {{regression.lecturenote.r}} | {{regression.lecturenote.r}} | ||
| + | |||
| + | < | ||
| + | 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, | ||
| + | # density graph | ||
| + | install.packages(" | ||
| + | library(ggplot2) | ||
| + | ggplot(data=df, | ||
| + | geom_histogram() + | ||
| + | geom_vline(aes(xintercept=mean(y)), | ||
| + | | ||
| + | coord_flip() | ||
| + | |||
| + | ggplot(data=df, | ||
| + | geom_density(color=" | ||
| + | geom_vline(aes(xintercept=mean(y)), | ||
| + | | ||
| + | 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, | ||
| + | geom_point(color=" | ||
| + | geom_hline(aes(yintercept=mean(y))) + | ||
| + | geom_abline(intercept=inc.y, | ||
| + | |||
| + | |||
| + | ggplot(data=df, | ||
| + | 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 | ||
| + | y <- 1.4*x + 2 + rnorm(sn, 0, 16) | ||
| + | y | ||
| + | df <- data.frame(x, | ||
| + | # density graph | ||
| + | ggplot(data=df, | ||
| + | geom_histogram() + | ||
| + | geom_vline(aes(xintercept=mean(y)), | ||
| + | | ||
| + | coord_flip() | ||
| + | |||
| + | ggplot(data=df, | ||
| + | geom_density(color=" | ||
| + | geom_vline(aes(xintercept=mean(y)), | ||
| + | | ||
| + | coord_flip() | ||
| + | |||
| + | |||
| + | ggplot(data=df, | ||
| + | geom_point(color=" | ||
| + | geom_hline(aes(yintercept=mean(y)), | ||
| + | geom_abline(intercept=10, | ||
| + | |||
| + | 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, | ||
| + | geom_point(color=" | ||
| + | geom_hline(aes(yintercept=mean(y)), | ||
| + | geom_abline(intercept=a, | ||
| + | |||
| + | lm.mod2$residuals | ||
| + | sum(lm.mod2$residuals^2) | ||
| + | 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) | ||
| + | |||
| + | ### | ||
| + | ggplot(data=df, | ||
| + | geom_point(color=" | ||
| + | geom_hline(aes(yintercept=mean(y)), | ||
| + | stat_smooth(method = " | ||
| + | formula = y ~ x, | ||
| + | geom = " | ||
| + | |||
| + | |||
| + | |||
| + | </ | ||
c/ma/regression_lecture_note.1698412706.txt.gz · Last modified: by hkimscil
