R code demonstrated at
6PAS2 Probability and Statistic 2, summer semester 2025/2026.
Class 2 (Feb 18 2026)
raw data (air pollution and temperature at
Ostrava-Českobratrská during 24 hour period on Sep 20 2015)
pm25 <- c(5, 5, 7, 8, 9, 10, 8, 6, 6, 6, 6, 10, 11, 11, 13, 12, 12, 15, 14, 12, 11, 12, 14, 13)
temp <- c(10, 10, 10, 10, 10, 10, 10, 8.5, 8.5, 8.5, 7, 6.5, 6.5, 7, 7, 7, 8, 6, 5.5, 5.5, 5.5, 4, 4, 5.5)
Displaying histogram of pm25:
1st way:
hhist <- function (v)
{
u <- unique(sort(v));
n <- length(u);
d <- rep (0, times=length(u));
for (i in (1:n))
{
for (val in v)
{
if (val == u[i])
{
d[i] <- d[i] + 1;
}
}
}
plot (u,d, lwd=10, type="h");
}
2nd way:
u <- unique(sort(pm25));
n <- length(u);
d <- rep (0, times=length(u));
for (i in (1:n))
{
d[i] = sum(pm25 == u[i]);
}
plot (u,d, lwd=10, type="h")
3rd way: hist (pm25)
Plotting histogram of random normally distributed sample:
hist (rnorm (1000), 100)
Plot air pollution against temerature:
plot (temp, pm25)
abline (lm (pm25 ~ temp))
Plotting graph of a function:
x <- seq (-5,5,length=100)
plot (x, sin(x), type="l")
Normal distribution:
Norm <- function (x,sigma,m)
{
return (1/(sigma * sqrt(2*pi)) * exp(-0.5*((x-m)/sigma)^2));
}
Plotting 3 normal distributions together:
plot (x, Norm(x,1,0), type="l")
lines (x, Norm(x,2,0), type="l", col="green")
lines (x, Norm(x,1,4), type="l", col="blue")
Load package MASS. Plotting histogram of SP500 data from there against normal
distribution:
library (MASS)
hist (SP500, 100, freq=F)
lines (x, Norm(x, sd(SP500), mean(SP500)), type="l", col="blue")
lines (x, Norm(x, 0.8*sd(SP500), mean(SP500)), type="l", col="blue")
Laplace distribution:
Lap <- function (x,b,m)
{
return (1/(2*b) * exp(- abs(x-m)/b));
}
lines (x, Lap(x, sd(SP500)/sqrt(2), mean(SP500)), type="l", col="green")
Logistic distribution:
Log <- function (x,s,m)
{
f <- -(x-m)/s;
e <- exp(f);
return (e/(s*(1+e)^2));
}
lines (x, Log(x, sd(SP500)*sqrt(3)/pi, mean(SP500)), type="l", col="red")
Created: Wed Sep 30 2015
Last modified: Wed Apr 1 2026 19:15:52 CEST