c:ms:regression_lecture_note_for_r
This is an old revision of the document!
################ rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) } n <- 900 set.seed(101) x <- rnorm2(n, 100, 10) rand.00 <- rnorm(n, 0, 12) rand.01 <- rnorm(n, 0, 240) plot(rand.01) points(rand.00, col="red") b = 170 / 265 b y <- b * x + rand.00 df <- data.frame(x, y) head(df) # plot method 0 plot(x,y) # method 1 plot(x, y, pch = 1, cex = 1, col = "black", main = "HEIGHT against SHOE SIZE", xlab = "SHOE SIZE", ylab = "HEIGHT (cm)") abline(h = mean(y), lwd=1.5, col="red") abline(lm(y ~ x), lwd=1.5, col="blue") # method 2 lm.mod <- lm(y ~ x, data = df) summary(lm.mod) str(lm.mod) intercept <- lm.mod$coefficients[1] slope <- lm.mod$coefficients[2] intercept slope 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=intercept, slope=slope) ##### ss.y <- sum((y-mean(y))^2) ss.y df.y <- length(y)-1 df.y ss.y/df.y var(y) cov.val <- cov(x,y) m.x <- mean(x) m.y <- mean(y) sp <- sum((x-m.x)*(y-m.y)) df.tot <- n-1 sp/df.tot cov.val sd.x <- sd(x) sd.y <- sd(y) r.cal <- cov.val/(sd.x*sd.y) r.cal cor(x,y) cor.test(x,y) b <- cov(x,y) / var(x) # m.y = a + b * mean.x a <- m.y - (b * m.x) b a lm.mod <- lm(y ~ x, data=df) summary(lm.mod) lm.xy$coefficients y.pred <- lm.xy$fitted.values y <- y m.y residual <- y - y.pred explained <- y.pred - m.y ss.y ss.res <- sum(residual^2) ss.reg <- sum(explained^2) ss.y ss.res ss.reg ss.res + ss.reg r.square <- ss.reg / ss.y r.square ss.tot <- ss.y ss.tot ss.reg ss.res df.tot <- df.y df.reg <- 2 - 1 df.res <- df.tot - df.reg df.reg df.res df.tot ss.reg ss.res ss.tot ms.reg <- ss.reg / df.reg ms.res <- ss.res / df.res ms.reg ms.res f.cal <- ms.reg/ms.res f.cal p.val <- pf(f.cal, df.reg, df.res, lower.tail = F) p.val anova(lm.xy) r.cal^2
c/ms/regression_lecture_note_for_r.1716335993.txt.gz · Last modified: 2024/05/22 08:59 by hkimscil