0% found this document useful (0 votes)
14 views

1 Introduction To R and Rstudio: 2024-2025 Calculus Iii

Uploaded by

tenshi5653
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

1 Introduction To R and Rstudio: 2024-2025 Calculus Iii

Uploaded by

tenshi5653
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Université Internationale de Rabat Academic Year 2024–2025

School of Aerospace & Automotive Engineering Calculus III

Practical Session 1
Univariate and Bivariate Descriptive Statistics

1 Introduction to R and RStudio


R is a programming language used extensively for statistical computing and data analysis. It
provides a wide variety of statistical techniques such as linear and nonlinear modeling, time-series
analysis, classification, and more. R is open-source, which makes it accessible to anyone.
RStudio is an integrated development environment (IDE) for R. It enhances the experience of
working with R by offering a user-friendly interface with several useful features. RStudio contains
four main panels:

ˆ 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.

ˆ Plots/Files/Packages/Help/Viewer: This panel provides different functionalities, including


viewing plots, managing files and packages, accessing help documentation, and viewing other
outputs.

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:

var_x <- var(x)


var_y <- var(y)
cat("Variance of x:", (var_x/10)*9, "\n")
cat("Variance of y:", (var_y/10)*9, "\n")

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_xy <- cov(x, y)


cat("Covariance between x and y:", (cov_xy/10)*9, "\n")

2.4 Correlation Coefficient


The correlation coefficient between x and y is computed as:

Cov(x, y)
Correlation(x, y) = p
Var(x) · Var(y)
R Code:

cor_xy <- cor(x, y)


cat("Correlation coefficient between x and y:", cor_xy, "\n")

2.5 Linear Regression


The linear regression model is given by the equation:

y = β0 + β1 x + ϵ
Where β0 is the intercept, and β1 is the slope of the regression line.
R Code:

regression <- lm(y ~ x)


cat("Regression coefficients:\n")
print(summary(regression))

2
2.6 Visualization
We will plot a scatter plot of x vs. y and overlay the regression line.
R Code:

plot(x, y, main = "Linear Regression of y on x",


xlab = "Annual Income (x in 10^4 DH)", ylab = "Savings (y in 10^4 DH)",
pch = 19, col = "blue", cex = 1.5)
abline(regression, col = "red", lwd = 2)
legend("topleft", legend = c("Points", "Regression Line"),
col = c("blue", "red"), pch = c(19, NA), lwd = c(NA, 2))

2.7 Prediction
Finally, we will predict the value of y for x = 11.
R Code:

x_new <- data.frame(x = 11)


y_pred <- predict(regression, newdata = x_new)
cat("The prediction for x = 11 is:", y_pred, "\n")

You might also like