#课堂作业 理解mpg 数据 变换 x
1+1
## [1] 2
print("Hello world")
## [1] "Hello world"
#install.packages(tidyverse)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.4 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(broom)
View(mpg)
names(mpg)
## [1] "manufacturer" "model" "displ" "year" "cyl"
## [6] "trans" "drv" "cty" "hwy" "fl"
## [11] "class"
# estimate and print the linear model
lm(hwy ~ displ, data = mpg) %>%
tidy() %>%
mutate(term = c("Intercept", "Engine displacement (in liters)")) %>%
knitr::kable(
digits = 2,
col.names = c(
"Variable", "Estimate", "Standard Error",
"T-statistic", "P-Value"
)
)
| Intercept |
35.70 |
0.72 |
49.55 |
0 |
| Engine displacement (in liters) |
-3.53 |
0.19 |
-18.15 |
0 |
ggplot(data = mpg, aes(displ, hwy)) +
geom_point(aes(color = class)) +
geom_smooth(method = "lm", se = FALSE, color = "black", alpha = .25) +
labs(
x = "Engine displacement (in liters)",
y = "Highway miles per gallon",
color = "Car type"
) +
theme_bw(base_size = 16)
## `geom_smooth()` using formula 'y ~ x'

lm(hwy ~ cyl, data = mpg) %>%
tidy() %>%
mutate(term = c("Intercept", "number of cylinders")) %>%
knitr::kable(
digits = 2,
col.names = c(
"Variable", "Estimate", "Standard Error",
"T-statistic", "P-Value"
)
)
| Intercept |
40.02 |
0.96 |
41.72 |
0 |
| number of cylinders |
-2.82 |
0.16 |
-17.92 |
0 |
ggplot(data = mpg, aes(cyl, hwy)) +
geom_point(aes(color = class)) +
geom_smooth(method = "lm", se = FALSE, color = "black", alpha = .25) +
labs(
x = "number of cylinders",
y = "Highway miles per gallon",
color = "Car type"
) +
theme_bw(base_size = 16)
## `geom_smooth()` using formula 'y ~ x'
