-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.R
55 lines (49 loc) · 1.62 KB
/
code.R
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
data(mtcars)
summary(mtcars)
?mtcars
mtcars$am <- factor(mtcars$am)
mtcars$vs <- factor(mtcars$vs)
library(reshape)
meltedmtcars <- melt(mtcars, id.vars = c("mpg", "am"))
qplot(value, mpg, facets = variable~. , color=am, data=meltedmtcars)
p <- ggplot(meltedmtcars, aes(x=value, y=mpg, color=am)) + geom_point()
p + facet_wrap( ~ variable, ncol = 3)
plot(mtcars$qsec, mtcars$mpg)
fitlm <- lm(mpg~am, data=mtcars)
plot(fitlm)
coef(fitlm)
confint(fitlm2)
n <- which(abs(hatvalues(fitlm) - summary(hatvalues(fitlm))[6]) < .01)
fitlm2 <- lm(mpg ~., data=mtcars[-n,])
fitglm <- glm(mpg~., data=mtcars, family = gaussian)
plot(predict(fitglm), resid(fitglm))
par(mfrow= c(2,2))
for (i in 2:dim(mtcars)[2]){
plot(mtcars[,i], mtcars$mpg, xlab=names(mtcars)[i],
ylab ="MPG")
}
hist(mtcars$mpg)
plot(fitlm)
summary(hatvalues(fitlm))
l <- list()
for (i in 2:dim(mtcars)[2]){
formula <- paste("mpg", "~","+am+", paste(sapply(2:i, function(j) names(mtcars)[j]), collapse = "+"))
l[i] <- lm(formula, data = mtcars)
}
anova(sapply(1:length(l), function(i) l[[i]]))
anova(l[1], l[2])
library(caret)
corMatrix <- cor(mtcars[,- c(1,8,9)])
library(car)
vif(fitlm2)
highlyCorrelated <- findCorrelation(corMatrix, cutoff = .8)
newdata <- mtcars[, -(highlyCorrelated+1)]
fitlm <- lm(paste("mpg~",paste(sapply(3:ncol(mtcars), function(j) names(mtcars)[j]), collapse = "+")), data=mtcars)
fitlm2 <- lm(mpg ~., data=newdata)
coef(fitlm2)
confint(fitlm2, level = .8)
lm <- train(mpg ~ . , method="lm", data=newdata)
lm
coef(lm$finalModel)
confint(lm$finalModel)
t.test(mtcars$mpg[mtcars$am == 0], mtcars$mpg[mtcars$am == 1], paired = F)