02-10-2026 >eR-BioStat and STAEM-3D
Modeling infectious diseases using R - basic programming in R
Ziv Shkedy, Rashider Aloni and Leyla Kodalci
## load/install libraries
.libPaths(c("./Rpackages",.libPaths()))
library(knitr)
library(tidyverse)
library(deSolve)
library(minpack.lm)
library(ggpubr)
library(readxl)
library(gamlss)
library(data.table)
library(grid)
library(png)
library(nlme)
library(gridExtra)
library(mvtnorm)
library(e1071)
library(lattice)
library(ggplot2)
library(dslabs)
library(NHANES)
library(plyr)
library(dplyr)
library(nasaweather)
library(ggplot2)
library(gganimate)
library(av)
library(gifski)
library(foreach)
library("DAAG")
library(DT)
library(TeachingDemos)
library(gridExtra)
We consider two vectors, \(x\) and \(y\):
\(x=(1.9,1.2,0.7,2.7,1.2,3.1,2.3,2.1,2.1,1.4)\) and \(y=(1.8,1.1,0.6,2.7,1.4,3.0,2.5,1.9,1.8,1.4)\).
We difine the two vectors in R in teh follwoing way. For \(x\):
x<-c(1.9,1.2,0.7,2.7,1.2,3.1,2.3,2.1,2.1,1.4)
x
## [1] 1.9 1.2 0.7 2.7 1.2 3.1 2.3 2.1 2.1 1.4
and for \(y\):
y<-c(1.8,1.1,0.6,2.7,1.4,3.0,2.5,1.9,1.8,1.4)
y
## [1] 1.8 1.1 0.6 2.7 1.4 3.0 2.5 1.9 1.8 1.4
Our first data analysis task is to calculate the mean of \(x\) and \(y\). In R, the function mean() can be used to calculate the sample mean. In our example we use mean(x) and mean(y):
mean(x)
## [1] 1.87
mean(y)
## [1] 1.82
Our second data analysis task if to calculate and vizualize the corelation between \(x\) and \(y\). To calculate the corelation we use the function cor().
cor(x,y)
## [1] 0.9774492
We use the function plot to produce the scaterplot.
plot(x,y)
A function is R. is a code that can be used to produce output. For example, suppose that we want to calculate (1) the mean of two vectors, (2) the corelation between two vectors and (3) to plot a scaterplot. We can wrtie a R function, discript that can be used to produce the output.
discript<-function(x,y)
{
print(mean(x))
print(mean(y))
print(cor(x,y))
plot(x,y)
}
To apply the function to the the vectors \(x\) and \(y\) we use discript(x,y).
discript(x,y)
## [1] 1.87
## [1] 1.82
## [1] 0.9774492
The cars dataset contains information about cars’ speed and cars’ stoping distance.
head(cars)
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
The first column is the cars’ speed and the second column is the cars’ stoping distance. Our data analysis task is
discript(cars[,1],cars[,2])
## [1] 15.4
## [1] 42.98
## [1] 0.8068949