1 Introduction To R and Rstudio: 2024-2025 Calculus Iii
1 Introduction To R and Rstudio: 2024-2025 Calculus Iii
Practical Session 1
Univariate and Bivariate Descriptive Statistics
Source Panel: This is where the code is written and edited. You can write scripts, functions,
and markdown documents here.
Console: The console is where R code is executed interactively. You can run single commands
here and immediately see the results.
Environment/History: The Environment tab shows all the variables, functions, and data
currently loaded into memory. The History tab keeps track of all the commands that have
been executed.
You can download R from the official website: https://cran.r-project.org/, and RStudio
from: https://posit.co/download/rstudio-desktop/.
2 Regression Exercise
In this exercise, we will perform simple linear regression using the following data:
Data:
x <- c(12, 15, 13, 10, 10, 14, 16, 18, 16, 14)
y <- c(0.2, 1.2, 1.0, 0.7, 0.3, 1.0, 1.6, 1.4, 1.2, 0.7)
2.1 Means
We calculate the means of the variables x and y:
n
1X
mean x = xi
n i=1
n
1X
mean y = yi
n i=1
R Code:
1
mean_x <- mean(x)
mean_y <- mean(y)
cat("Mean of x:", mean_x, "\n")
cat("Mean of y:", mean_y, "\n")
2.2 Variances
The variances of x and y are calculated as follows:
n
1 X
Variance of x = (xi − mean x)2
n − 1 i=1
R Code:
2.3 Covariance
The covariance between x and y is given by:
n
1 X
Cov(x, y) = (xi − mean x)(yi − mean y)
n − 1 i=1
R Code:
Cov(x, y)
Correlation(x, y) = p
Var(x) · Var(y)
R Code:
y = β0 + β1 x + ϵ
Where β0 is the intercept, and β1 is the slope of the regression line.
R Code:
2
2.6 Visualization
We will plot a scatter plot of x vs. y and overlay the regression line.
R Code:
2.7 Prediction
Finally, we will predict the value of y for x = 11.
R Code: