library(ISLR) attach(Carseats) str(Carseats) m.Sales.PriceShelveLoc <- lm(Sales ~ Price + ShelveLoc, data=Carseats) summary(m.Sales.PriceShelveLoc) plot(Price[ShelveLoc=="Bad"], Sales[ShelveLoc=="Bad"], col="blue", xlab="Price", ylab="Sales", main="Sales vs. Price, Shlv Loc") points(Price[ShelveLoc=="Medium"], Sales[ShelveLoc=="Medium"], col="red") points(Price[ShelveLoc=="Good"], Sales[ShelveLoc=="Good"], col="green") legend("topright", legend=c("Good", "Medium", "Bad"), col=c("green", "red", "blue"), pch=c(1,16), bty="n") abline(a = 16.89765, b=-0.056698, col = "green", lwd=3) abline(a = 13.86382, b=-0.056698, col = "red", lwd=3) abline(a = 11.832984, b=-0.056698, col = "blue", lwd=3) 독립변인 간의 상호작용효과 보기 m.Sales.PriceShelveLoc.ia <- lm(Sales ~ Price + ShelveLoc + Price:ShelveLoc, data=Carseats) summary(m.Sales.PriceShelveLoc.ia) plot(Price[ShelveLoc=="Bad"], Sales[ShelveLoc=="Bad"], col="blue", xlab="Price", ylab="Sales", main="Sales vs. Price, Shlv Loc") points(Price[ShelveLoc=="Medium"], Sales[ShelveLoc=="Medium"], col="red") points(Price[ShelveLoc=="Good"], Sales[ShelveLoc=="Good"], col="green") legend("topright", legend=c("Good", "Medium", "Bad"), col=c("green", "red", "blue"), pch=c(1,16), bty="n") good <- 11.832984 + 6.135880 medium <- 11.832984 + 1.630481 bad <- 11.832984 p.good <- -0.055220 + -0.010564 p.medium <- -0.055220 + 0.001984 p.bad <- -0.055220 abline(a = good, b= p.good, col = "green", lwd=3) abline(a = medium, b= p.medium, col = "red", lwd=3) abline(a = bad, b= p.bad, col = "blue", lwd=3)