This is Quantitative Decision Making for Business by using R studio.

profileShelby1
ch19_R1.R

## ----setup, include=FALSE------------------------------------------------ knitr::opts_chunk$set(echo = TRUE) knitr::opts_chunk$set(fig.width=7, fig.height=3.3) ## ------------------------------------------------------------------------ diamond <- read.table('Diamonds_DGK_FE18.txt', sep = '\t', header = TRUE) ## ------------------------------------------------------------------------ str(diamond) ## ------------------------------------------------------------------------ par(mfrow = c(1,2)) plot(diamond$Carat.Weight, diamond$Price, xlab = 'Carat Weight', ylab = 'Price') plot(diamond$Carat.Weight, diamond$LogPrice, xlab = 'Carat Weight', ylab = 'Log10Price') ## ------------------------------------------------------------------------ library(ggplot2) ggplot(diamond, aes(Carat.Weight, LogPrice, color = Color)) + geom_point() ## ------------------------------------------------------------------------ imod1 <- lm(LogPrice ~ Carat.Weight + Color, data = diamond) ## ------------------------------------------------------------------------ summary(imod1) ## ------------------------------------------------------------------------ summary(lm(LogPrice ~ Carat.Weight + Color + Carat.Weight*Color, data = diamond)) ## ------------------------------------------------------------------------ par(mfrow = c(1,2)) plot(factor(diamond$Table), diamond$LogPrice, xlab = 'Table', ylab = 'LogPrice') plot(diamond$Depth, diamond$LogPrice, xlab = 'Depth', ylab = 'LogPrice') ## ------------------------------------------------------------------------ imod2 <- lm(LogPrice ~ Carat.Weight + Color + Clarity + Cut, data = diamond) ## ------------------------------------------------------------------------ summary(imod2) ## ------------------------------------------------------------------------ plot(imod2$fitted.values, imod2$residuals, xlab = 'Fitted values', ylab = 'Residuals') abline(0, 0) ## ------------------------------------------------------------------------ imod3.tmp <- lm(LogPrice ~ Carat.Weight + I(Carat.Weight^2) + Color + Clarity + Cut, data = diamond) summary(imod3.tmp) ## ------------------------------------------------------------------------ library(car) vif(imod3.tmp) ## ------------------------------------------------------------------------ cor(diamond$Carat.Weight, (diamond$Carat.Weight)^2) ## ------------------------------------------------------------------------ diamond$Carat.Weight2 <-(diamond$Carat.Weight - mean(diamond$Carat.Weight))^2 ## ------------------------------------------------------------------------ imod3 <- lm(LogPrice ~ Carat.Weight + Carat.Weight2 + Color + Clarity + Cut, data = diamond) vif(imod3) ## ------------------------------------------------------------------------ summary(imod3) ## ------------------------------------------------------------------------ par(mfrow = c(1, 2)) plot(imod3$fitted.values, imod3$residuals, xlab = 'Fitted values', ylab = 'Residuals') abline(0, 0) hist(imod3$residuals, main = "Histogram of residuals", xlab = 'Residuals') ## ------------------------------------------------------------------------ std.res <- rstandard(imod3) lev <- hatvalues(imod3) par(mfrow = c(1,2)) hist(std.res, xlab = 'Standardized residuals', main = "") hist(lev, xlab = 'Leverages', main = "") ## ------------------------------------------------------------------------ diamond[which(abs(std.res) > 3), c(2, 3, 4, 5, 8)] ## ------------------------------------------------------------------------ lev.cut <- 3*length(imod3$coefficients)/dim(diamond)[1] diamond[which(lev > lev.cut), c(2, 3, 4, 5, 8)] ## ------------------------------------------------------------------------ cookd <- cooks.distance(imod3) dffit <- dffits(imod3) par(mfrow = c(1,2)) boxplot(cookd, xlab = "Cook's distance") boxplot(dffit, xlab = 'DFFITS') ## ------------------------------------------------------------------------ diamond[which.max(cookd), c(2, 3, 4, 5, 8)] ## ------------------------------------------------------------------------ dffit.cut <- 2*sqrt(length(imod3$coefficients)/ (dim(diamond)[1] - length(imod3$coefficients))) ## ------------------------------------------------------------------------ which(abs(dffit) > dffit.cut) ## ------------------------------------------------------------------------ diamond[which.max(abs(dffit)), c(2, 3, 4, 5, 8)] ## ------------------------------------------------------------------------ null <- lm(LogPrice ~ 1, data = diamond) full <- lm(LogPrice ~ Carat.Weight + Carat.Weight2 + Color + Clarity + Cut + Depth + Table , data = diamond) ## ------------------------------------------------------------------------ step.reg <- step(null, scope=list(lower=null, upper=full), test = 'F') ## ------------------------------------------------------------------------ summary(step.reg)