R code used at VAPMS Selected Applications of Mathematical Statistics, summer semester 2018/2019.

Class 4 (Mar 7 2019)

Function for computing standard deviation with "n" in denominator, from Homework 2:
sd1 <- function (x)
{
	m <- mean(x);
	sqrt (sum ((x - m)^2) / length(x));
}
Computation of the 95% confidence interval for a sample of 5 out of normal distribution with mean 83 and standard deviation 12:
mean <- 83
sigma <- 12
n <- 5
stderr <- sigma/sqrt(n)
mean + stderr * qnorm (0.025)
mean + stderr * qnorm (0.975)
Function computing normal scores:
# take n values from v, m times
normscores <- function (v, n, m)
{
	s <- rep (0, n);
	for (i in 1:m)
	{
		s <- s + sort (sample (v, n));
	}
	return (s/m);
}
Computing normal scores using randomly generated sample:
normscores (rnorm (1000, mean=0, sd=1), 10, 100)

Computing normal scores using built-in R function (be sure that the package SuppDists is installed):
library (SuppDists)
normOrder (10)
Function for normality testing using normal scores:
testnorm <- function (v, n, m)
{
	a <- normscores (v, n, m);
	b <- sort (rnorm (n, mean=mean(v), sd=sd(v)));
	plot (b,a);
	abline (lm (a ~ b));
}
Test for normality SP500 (from MASS package):
testnorm (SP500, 10, 100)
testnorm (SP500, 100, 1000)
Function for normality testing using Q-Q plot:
QQ <- function (v)
{
	p <- seq (0.1, 0.9, by=0.1);
	qv <- quantile (v, p, names=F);
	qn <- qnorm (p, sd=sd(v), mean=mean(v));
	plot (qn,qv);
	abline (lm (qv ~ qn));
}
Test for normality SP500 using built-in R functions:
qqnorm (SP500)
qqline (SP500)
Produce Q-Q(-like) plot of one data against another (pm25 and temp are from Class 1):
qqplot (x=pm25, y=temp)
qqline (temp)

pm25_sorted <- sort(pm25)
temp_sorted <- sort(temp)
plot (pm25_sorted, temp_sorted)
slope <- diff(pm25_sorted)/diff(temp_sorted)
slope <- slope[is.finite(slope)]
abline (1, slope)

Created: Thu Oct 29 2015
Last modified: Thu Mar 7 18:13:38 CET 2019