# 直線迴歸 (General linear model)
###### tags: `R教學` `基礎統計`
```r=
rm(list=ls())
library("multcomp")
dat <- iris[,c(1,2,5)]
#### 繪圖 ####
tiff(filename = "lm.tif",
width = 350*3.5, height = 240*3.5, pointsize = 20)
plot(Sepal.Width ~ Sepal.Length, data = dat, las = 1)
abline(lm(Sepal.Width ~ Sepal.Length,
data = dat),
lwd = 3)
points(Sepal.Width ~ Sepal.Length, data = dat, col = Species, pch = 19)
abline(lm(Sepal.Width ~ Sepal.Length,
data = subset(dat, Species == levels(dat$Species)[1])),
col = 1, lty = 2)
abline(lm(Sepal.Width ~ Sepal.Length,
data = subset(dat, Species == levels(dat$Species)[2])),
col = 2, lty = 2)
abline(lm(Sepal.Width ~ Sepal.Length,
data = subset(dat, Species == levels(dat$Species)[3])),
col = 3, lty = 2)
legend("topleft",legend=levels(dat$Species),
pch = 19, col=1:3, lty=2, yjust=0, inset = 0.01)
dev.off()
#### 統計模型 ####
lm.model <- lm(Sepal.Width ~ Sepal.Length + Species, data = dat)
summary(lm.model)
anova(lm.model)
summary(Posthoc <- glht(lm.model, linfct = mcp(Species = "Tukey")))
(letter.dat <- cld(Posthoc,
alpha = 0.05,
Letters = letters,
decreasing = TRUE))
```
