library(ggplot2)
library(ggfortify)
data("PlantGrowth")
summary(PlantGrowth)
weight group
Min. :3.590 ctrl:10
1st Qu.:4.550 trt1:10
Median :5.155 trt2:10
Mean :5.073
3rd Qu.:5.530
Max. :6.310
str(PlantGrowth)
'data.frame': 30 obs. of 2 variables:
$ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
$ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
PlantGrowth$group <- as.factor(PlantGrowth$group)
str(PlantGrowth)
'data.frame': 30 obs. of 2 variables:
$ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
$ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
ggplot(PlantGrowth, aes(x = group, y = weight)) + geom_boxplot() + ylab("Plant weight") +
xlab("Treatment group")

ggplot(PlantGrowth, aes(x = group, y = weight)) + geom_boxplot() + geom_jitter(width = 0.08,
height = 0) + ylab("Plant weight") + xlab("Treatment group")

model_weight <- lm(weight ~ group, data = PlantGrowth)
anova(model_weight)
Analysis of Variance Table
Response: weight
Df Sum Sq Mean Sq F value Pr(>F)
group 2 3.7663 1.8832 4.8461 0.01591 *
Residuals 27 10.4921 0.3886
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model_weight)
Call:
lm(formula = weight ~ group, data = PlantGrowth)
Residuals:
Min 1Q Median 3Q Max
-1.0710 -0.4180 -0.0060 0.2627 1.3690
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.0320 0.1971 25.527 <2e-16 ***
grouptrt1 -0.3710 0.2788 -1.331 0.1944
grouptrt2 0.4940 0.2788 1.772 0.0877 .
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.6234 on 27 degrees of freedom
Multiple R-squared: 0.2641, Adjusted R-squared: 0.2096
F-statistic: 4.846 on 2 and 27 DF, p-value: 0.01591
autoplot(model_weight)

ggplot(PlantGrowth, aes(x = group, y = weight)) + geom_boxplot(outlier.shape = NA) +
geom_jitter(width = 0.08, height = 0, alpha = 0.7) + stat_summary(fun = mean,
geom = "point", size = 3) + stat_summary(fun.data = mean_cl_normal, geom = "errorbar",
width = 0.15) + ylab("Plant weight") + xlab("Treatment group")

data("mtcars")
summary(mtcars[, c("mpg", "wt")])
mpg wt
Min. :10.40 Min. :1.513
1st Qu.:15.43 1st Qu.:2.581
Median :19.20 Median :3.325
Mean :20.09 Mean :3.217
3rd Qu.:22.80 3rd Qu.:3.610
Max. :33.90 Max. :5.424
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + xlab("Weight (1000 lbs)") +
ylab("Fuel efficiency (mpg)")

model_mpg <- lm(mpg ~ wt, data = mtcars)
summary(model_mpg)
Call:
lm(formula = mpg ~ wt, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.5432 -2.3647 -0.1252 1.4096 6.8727
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
wt -5.3445 0.5591 -9.559 1.29e-10 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
autoplot(model_mpg)

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_smooth(method = "lm",
se = TRUE) + xlab("Weight (1000 lbs)") + ylab("Fuel efficiency (mpg)")
