R Programming Fundamentals and Statistical Analysis

R Basics

Help and Function Documentation

?lm
help(lm)
help.start()

help.search("lm")
RSiteSearch("regression")
example(lm)

Installing Packages in R

install.packages('sos')

Loading Packages

library(sos)

z <- findFn({'Control Charts'})
z
findFn('Control Charts')

Visualization of Historical Commands

history()

Important Data Types

a <- c(1,2,3)
b <- c(4,6,8)
cbind(a,b)
rbind(a,b)

Mean Calculation (1)

omean <- function(v) {
m <- mean(v)
ifelse(is.na(m), 0, m)
}

Mean Calculation (2)

omean <- function(v) {
m <- mean(v)
ifelse(is.na(m), mean(na.omit(v)), m)
}

Function for Mean-Median Deviation & Histogram

normalita <- function(x){
print(mean(x) - median(x))
hist(x)
}

Dice Roll Simulation

x <- vector(length = 10000)
for (i in 1:10000) {
  set.seed(i)
  x1 <- sample(1:6, 2, replace = TRUE)  # Corrected: No need for round, sample returns integers
  x[i] <- sum(x1)
}
table(x)
barplot(table(x))

Data Export

write.table(dat, "risultati.csv")
write.table(dat, 'clipboard')

Table Creation in LaTeX

xtable(dat)

Multivariate Analysis

analisi_relazione <- function(y, x) {
plot(x, y)
cor(x, y)
reg1 <- lm(y ~ x)
par(cex = 0.8)
plot(x, y)
abline(reg1)
}

x <- dat[, 3]
y <- dat[, 4]
analisi_relazione(y, x)
apply(dat, 2, analisi_grafica)
cor(dat)
plot(dat)

Random Number Generators in R

x1 <- rnorm(100, 0, 1)
x2 <- runif(10, min = 1, max = 6)
x3 <- sample(1:6, 3, replace = FALSE)
x4 <- sample(1:6, 20, replace = TRUE)
risultati <- c("uno", "due", "tre")
x5 <- sample(risultati, 10, replace = TRUE)

From R Documentation

require(stats)
centre <- function(x, type) {
  switch(type,
         mean = mean(x),
         median = median(x),
         trimmed = mean(x, trim = 0.1))
}

USArrests
attach(USArrests)
head(USArrests)
x <- Murder
centre(x, "mean")
centre(x, "median")
centre(x, "trimmed")

Executing Commands from an External File

source("comandi.R")

Redirecting R Output to a File

sink("risultati.txt")
sink()

Creating Functions

somma <- function(x1, x2) {
print(x1 + x2)
}
x <- 10
y <- 10
somma(x, y) # Function call

Creating and Manipulating Vectors

y <- c(34, 45, 54, 56, 45, 67, 34, 45)
# Vector combination (creates a matrix)
cbind(y, y, y) # Horizontally
rbind(y, y, y) # Vertically
# Accessing vector elements
y[1]
# Modifying the vector
y[-1]
y[-2]
z <- y[-1] # Removing elements
z <- y[-2]
y <- c(2, y) # Adding elements

Betting Simulation

x <- vector(length = 10000)
for (i in 1:10000) {
  giocatore1 <- round(runif(1, min = 0, max = 6), 0)
  banco <- round(runif(1, min = 0, max = 6), 0)
  risultato <- ifelse(giocatore1 > banco, 100, -100)
  x[i] <- risultato
}
hist(x)

Queue Simulation

x <- vector(length = 10)
for (t in 1:10) {
  f <- ifelse(t == 1, 0, f)
  set.seed(t)
  f <- rpois(1, lambda = 12)
  g <- 4
  f <- f - g
  f <- ifelse(f < 0, 0, f) #Corrected the logic
  x[t] <- f
}
plot(1:10, x, type = 'l')

Apply and Lapply

dat <- read.delim('clipboard', header = FALSE)
dat2 <- data.frame(pre = dat$V1, post = dat$V2)
ts.plot(dat2)
apply(dat2, 2, mean)
apply(dat2, 2, var)
apply(dat2, 2, sd)
x1 <- dat2$pre[1:6]
x2 <- dat2$pre[7:12]
x3 <- dat2$pre[13:18]
x4 <- dat2$pre[19:24]
lis <- list(x1, x2, x3, x4)
lapply(lis, mean)
lapply(lis, var)

Univariate Analysis

summary(dat)
apply(dat, 2, mean)
apply(dat, 2, median)
apply(dat, 2, var)
apply(dat, 2, sd)
analisi_grafica <- function(x) {
hist(x)
plot(density(x))
boxplot(x)
}
analisi_grafica(dat[, 1])
analisi_grafica(dat[, 2])

Simple Regression – Liquid Cooling (Simulated Data)

x <- 1:40
x2 <- x * x
y <- 10.2 - 1.32 * x - 0.3 * x^2 + 0.1 * rnorm(40)
plot(x, y)
lm.D9 <- lm(y ~ x + x2 + 1)
summary(lm.D9)
plot(x, y)
lm.D9$coefficients
hist(lm.D9$residuals)
lm.D9 <- lm(y ~ x)
summary(lm.D9)
plot(x,y)
lm.D9$coefficients
hist(lm.D9$residuals)
plot(lm.D9$residuals)

Matrix Operations

Given the matrix A:

A
# Accessing element (2, 3)
A[3, 2]
# Selecting the first row
A[1, ]
# Selecting the second column
A[, 2]

Missing Data Handling

colMeans(x, na.rm = TRUE)
# Removing missing data
b = is.na(x)
s <- ifelse(b != FALSE, "ok", "no")
mean(x[s != "ok"])

Imputation with the Mean

b = is.na(x)