# 複因子直線迴歸模型 (two factor General linear model) ###### tags: `R教學` `基礎統計` 參考資料: 1. [Linear Models with R](http://www.utstat.toronto.edu/~brunner/books/LinearModelsWithR.pdf) 2. [Regression With Factor Variables](http://courses.atlas.illinois.edu/spring2016/STAT/STAT200/RProgramming/RegressionFactors.html) 3. [迴歸分析筆記 之 虛擬變數 Dummy Variable](https://belleaya.pixnet.net/blog/post/30877354) ```r= rm(list=ls()) # model simulation: y = 5x + 10 x1 <- rnorm(50, 10, 4) y1 <- x1*5 + 10 + rnorm(50, 0, 5) x2 <- rnorm(50, 30, 4) y2 <- x2*5 + 10 + rnorm(50, 0, 5) x3 <- rnorm(50, 10, 4) y3 <- x3*5 + 10 + rnorm(50, 0, 5) sd(y1); sd(y2); sd(y3) dat <- c(y1, y2, y3) x <- c(x1, x2, x3) trt <- rep(c("A", "B", "C"), each = 50) hist(dat, breaks = 30) hist(y1, breaks = 20, add = T, col = 2) hist(y2, breaks = 20, add = T, col = 3) plot(y1 ~ x1, xlim = c(0,50), ylim = c(0,250), col = 1, pch = 19) points(y2 ~ x2, col = 2, pch = 19) points(y3 ~ x3, col = 3, pch = 19) abline (10,5) t.test(y1,y2) summary(model <- (lm (dat ~ x + trt))) summary(lm (dat ~ trt)) mean(y2) - mean(y1) sqrt(sum((y1-mean(y1))^2)/50)/sqrt(50) ```