Here is the entire file. I don't think I have the right to add attachments, so I'm adding it inline.
title: 'Exercise XXX'
author: 'me'
date: '2020-05-29'
output:
pdf_document:
latex_engine: xelatex
instructions redacted
library(ISLR)
data(Auto)
attach(Auto)
str(Auto)
nrow(Auto)
ncol(Auto)
Auto <- na.omit(Auto)
nrow(Auto)
ncol(Auto)
colnames(Auto)
with(Auto,cylinders <- as.factor(cylinders))
with(Auto,origin <- as.factor(origin))
instructions redacted
summary(Auto)
table(Auto$mpg)
Quantitative: mpg, displacement, horsepower, weight, acceleration, year
Qualitative: cylinders, origin, name
instructions redacted
range(Auto$mpg)
range(Auto$displacement)
sapply(Auto[,c(1,3:7)],range)
instructions redacted
options(width=40)
options(digits=3)
sd(Auto$mpg)
sapply(Auto[,c(1,3:7)],mean)
sapply(Auto[,c(1,3:7)],sd)
as.data.frame(t(sapply(Auto[,c(1,3:7)],function(bla)
list(means=mean(bla),sds=sd(bla),ranges=range(bla)))))
instructions redacted
as.data.frame(t(sapply(Auto[-10:-85,c(1,3:7)],function(bla)
list(means=mean(bla),sds=sd(bla),ranges=range(bla)))))
instructions redacted
pairs(Auto[,c(1,3:7)])
library(GGally)
#install.packages("ggplot2")
#install.packages("fansi")
library(ggplot2)
ggpairs(Auto[,c(1,3:7)],aes(alpha=0.4))
plot(Auto$displacement,Auto$horsepower)
plot(Auto$weight,Auto$mpg)
plot(Auto$year,Auto$mpg)
The strongest linear relationship is between displacement and horsepower. The next strongest linear relationships seem to be between displacement and weight and between horsepower and weight. There is a strong categorical relationship between displacement and cylinders. There is a somewhat strong categorical relationship between displacement and origin. There are strong curvilinear relationships between mpg and displacement, mpg and horsepower, and mpg and weight. The other relationships are less clear.
instructions redacted
with(Auto,plot(weight,mpg))
with(Auto,plot(displacement,mpg))
with(Auto,plot(horsepower,mpg))
library(graphics)
with(Auto,scatter.smooth(weight,mpg))
p <- ggplot(Auto,aes(weight,mpg))+ geom_point()
p
p+geom_smooth(method=lm)
p+geom_smooth(method=loess)
Weight appears to be the best predictor of mpg. A scatterplot shows that only two 3500 lb+ cars get better than 20 mpg, while no 40 mpg+ car weighs more than 2500 lbs. Most cars show a relationship between mpg and weight on a smooth curve. Displacement and horsepower also appear to be good predictors, especially for larger values of displacement and horsepower.