-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexploratory.R
More file actions
62 lines (45 loc) · 1.73 KB
/
exploratory.R
File metadata and controls
62 lines (45 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
library(ggplot2)
library(GGally)
ds <- mtcars
str(ds)
head(ds)
ds$cyl <- as.factor(ds$cyl)
ds$vs <- as.factor(ds$vs) #V-engine or Straight-engine
ds$am <- factor(ds$am,labels=c('Automatic','Manual')) #Transmission-type
ds$gear <- as.factor(ds$gear)
ds$carb <- as.factor(ds$carb)
str(ds)
summary(ds)
#plot(ds)
pairs(ds, col=ds$am, panel = panel.smooth)
#boxplot(factor(ds$am, labels = c('a','m')),ds$mpg,col=c('lightgreen','lightblue'),xlab='transmission', ylab='mpg')
plot(as.factor(ds$am),ds$mpg,col=c('lightgreen','lightblue'), xlab='transmission', ylab='mpg')
abline(h = mean(ds$mpg[ds$am==0]),col='red')
abline(h = mean(ds$mpg[ds$am==1]),col='red')
fit <- lm(mpg ~ am,ds)
mdl <- lm(mpg ~ .,ds)
mdl2 <- lm(mpg ~ wt,ds)
summary(fit)
summary(mdl)
summary(mdl2)
par.orig <- par()
cols <- c(ds$am)
par(mfrow = c(2, 2), oma = c(0, 0, 2, 0))
plot(fit,col=alpha(cols,alpha=.5),pch=19)
plot(mdl,col=alpha(cols,alpha=.5),pch=19)
plot(mdl2,col=alpha(cols,alpha=.5),pch=19)
par(par.orig)
z <- lm(mpg ~ disp,ds)
plot(ds$disp,ds$mpg,pch=19,col=alpha(cols,alpha=.5),xlab='disp', ylab='mpg')
abline(z,col='blue')
plot(ds$hp,ds$mpg,pch=19,col=alpha(cols,alpha=.5),xlab='hp', ylab='mpg')
plot(ds$drat,ds$mpg,pch=19,col=alpha(cols,alpha=.5),xlab='drat', ylab='mpg')
z <- lm(mpg ~ wt,ds)
plot(ds$wt,ds$mpg,pch=19,col=alpha(cols,alpha=.5),xlab='weight', ylab='mpg')
abline(z,col='blue')
plot(ds$qsec,ds$mpg,pch=19,col=alpha(cols,alpha=.5),xlab='1/4 mile', ylab='mpg')
plot(ds$vs,ds$mpg,pch=19,col='lightgreen',xlab='vs', ylab='mpg')
plot(ds$cyl,ds$mpg,pch=19,col='lightgreen',xlab='cylinder', ylab='mpg')
plot(ds$gear,ds$mpg,pch=19,col='lightgreen',xlab='gear', ylab='mpg')
plot(ds$carb,ds$mpg,pch=19,col='lightgreen',xlab='carb', ylab='mpg')
pairs(ds,panel=panel.smooth)