-
Notifications
You must be signed in to change notification settings - Fork 125
/
ch-9.R
114 lines (70 loc) · 2.15 KB
/
ch-9.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Import packages that we need
library(tidyverse)
library(psych)
library(tidymodels)
# Read in the data, select only the columns we need
mpg <- read_csv("datasets/mpg/mpg.csv") %>%
select(mpg, weight, horsepower, origin, cylinders)
head(mpg)
# Descriptive statistics
describe(mpg)
# One-way frequency table of origin
mpg %>%
count(origin)
# Two-way frequency table
mpg %>%
count(origin, cylinders) %>%
pivot_wider(values_from = n, names_from = cylinders)
# Descriptive statistics by group
mpg %>%
select(mpg, origin) %>%
describeBy(group = 'origin')
# Histogram
ggplot(data = mpg, aes(x = mpg)) +
geom_histogram()
# Box plot
ggplot(data = mpg, aes(x = origin,y = mpg)) +
geom_boxplot()
# Facet histogram
ggplot(data = mpg, aes(x = mpg)) +
geom_histogram()+
facet_wrap(~ origin)
# Is there a difference in mileage?
mpg_filtered <- filter(mpg, origin == 'USA' | origin == 'Europe')
# Dependent variable ~ ("by") independent variable
t.test(mpg ~ origin, data = mpg_filtered)
select(mpg, mpg:horsepower) %>%
cor()
# Scatterplot
ggplot(data = mpg, aes(x = weight,y = mpg)) +
geom_point() + xlab("weight (pounds)") +
ylab("mileage (mpg)") + ggtitle("Relationship between weight and mileage")
# Pairplot
select(mpg, mpg:horsepower) %>%
pairs()
# Fit the regression, print the summary results
mpg_regression <- lm(mpg ~ weight, data = mpg)
summary(mpg_regression)
# Scatterplot with fit linear regression line
ggplot(data = mpg, aes(x = weight, y = mpg)) +
geom_point() + xlab("weight (pounds)") +
ylab("mileage (mpg)") + ggtitle("Relationship between weight and mileage") +
geom_smooth(method = lm)
set.seed(1234)
mpg_split <- initial_split(mpg)
mpg_train <- training(mpg_split)
mpg_test <- testing(mpg_split)
dim(mpg_train)
dim(mpg_test)
# Specify what kind of model this is
lm_spec <- linear_reg()
# Fit the model to the data
lm_fit <- lm_spec %>%
fit(mpg ~ weight, data = mpg_train)
tidy(lm_fit)
glance(lm_fit)
mpg_results <- predict(lm_fit, new_data = mpg_test) %>%
bind_cols(mpg_test)
mpg_results
rsq(data = mpg_results, truth = mpg, estimate = .pred)
rmse(data = mpg_results, truth = mpg, estimate = .pred)