R Markdown

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

DATA 667, Cars Example, Week 9, Erin Wright

write data into csv file

‘’’{r cars} write.csv(mtcars, file = “data/cars.csv”, row.names=FALSE) cars_data <- read.csv(file = “data/cars.csv”, header = TRUE, sep = “,”) #read file back in to use #cannot get this part to work yet, single dot works because that is current directory, #we didn’t write the file to the parent directory so “..” will not work #cars_data <- read.csv(file = “~/data/cars.csv”, header = TRUE, sep = “,”)

pairs plot

pairs(cars_data)

hypothesis test

cor(cars_data\(wt, cars_data\)mpg)

install ggplot2 and plot regression

install.packages(“ggplot2”)

require(ggplot2) ggplot(cars_data, aes(x=wt, y=mpg))+ geom_point(aes(shape=factor(am, labels = c(“Manual”,“Automatic”))))+ geom_smooth(method=lm)+scale_shape_discrete(name = “Transmission Type”) firstModel <- lm(mpg~wt, data = cars_data) #store in a variable

see summary and details

summary(firstModel)\(coef print(c('R-squared', round(summary(firstModel)\)r.sq,2)))

Test other correlations

completeModel <- lm(mpg ~., data=cars_data) stepSolution <- step(completeModel, direction = “backward”) #get the best model bestModel <- stepSolution$call bestModel

finalModel <- lm(mpg~wt + factor(am) + qsec, data = cars_data) summary(finalModel)\(coef print(c('R-squared', round(summary(finalModel)\)r.sq,2)))

cannot get the export to HTML to work, getting error (File>Compile Report)