我们首先尝试使用Rmarkdown进行统计图形的绘制。
library(ggplot2)
data(mpg)
head(mpg)
## # A tibble: 6 × 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
看完了统计图,我们再来尝试进行回归分析。
fit<- lm(cty ~ hwy, data = mpg)
fit
##
## Call:
## lm(formula = cty ~ hwy, data = mpg)
##
## Coefficients:
## (Intercept) hwy
## 0.8442 0.6832
summary(fit)
##
## Call:
## lm(formula = cty ~ hwy, data = mpg)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.9247 -0.7757 -0.0428 0.6965 4.6096
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.84420 0.33319 2.534 0.0119 *
## hwy 0.68322 0.01378 49.585 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.252 on 232 degrees of freedom
## Multiple R-squared: 0.9138, Adjusted R-squared: 0.9134
## F-statistic: 2459 on 1 and 232 DF, p-value: < 2.2e-16
plot(fit)