1 + 1
[1] 2
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
1 + 1
[1] 2
You can add options to executable code like this
[1] 4
The echo: false
option disables the printing of code (only output is displayed).
Now, I am loading my first dataset.
remove(list=ls())
datasets()
?
library(help = "datasets")
?Formaldehyde
<- Formaldehyde df
require(stats) # Loads the 'stats' package, which contains statistical functions like linear regression (lm).
require(graphics) # Loads the 'graphics' package, which provides plotting functions.
# Create a scatter plot of 'optden' (optical density) against 'carb' (carbohydrate) from the 'Formaldehyde' dataset.
plot(optden ~ carb, data = Formaldehyde,
xlab = "Carbohydrate (ml)", ylab = "Optical Density", # Set axis labels.
main = "Formaldehyde data", col = 4, las = 1) # Set the title, color of points (4 is blue), and axis label orientation (las=1 makes labels horizontal).
# Add the linear regression line to the plot.
abline(fm1 <- lm(optden ~ carb, data = Formaldehyde)) # 'lm' fits a linear model, and 'abline' adds the regression line. 'fm1' stores the model.
# Display the summary of the linear regression model.
summary(fm1) # Provides information about the model, such as coefficients, p-values, and R-squared.
Call:
lm(formula = optden ~ carb, data = Formaldehyde)
Residuals:
1 2 3 4 5 6
-0.006714 0.001029 0.002771 0.007143 0.007514 -0.011743
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.005086 0.007834 0.649 0.552
carb 0.876286 0.013535 64.744 3.41e-07 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.008649 on 4 degrees of freedom
Multiple R-squared: 0.999, Adjusted R-squared: 0.9988
F-statistic: 4192 on 1 and 4 DF, p-value: 3.409e-07
# Set up a 2x2 grid of plots.
<- par(mfrow = c(2, 2), oma = c(0, 0, 1.1, 0)) # 'par' sets or queries graphical parameters. 'mfrow' arranges plots in a grid. 'oma' sets outer margins. 'opar' stores the original parameters.
opar
# Generate diagnostic plots for the linear regression model.
plot(fm1) # Produces four plots: residuals vs. fitted values, Q-Q plot of residuals, scale-location plot, and residuals vs. leverage.
# Restore the original graphical parameters.
par(opar) # Resets the graphical parameters to their original values.