NONPARAMETRIC AND ROBUST STATISTICS - e

NONPARAMETRIC AND ROBUST STATISTICS WITH R – LAB 1
1
EXAMPLE 1
A packaging process is supposed to fill small Bowes of raisins with approximately 50 raisins so that
each box will weigh the same. However, the number of raisins in each box will vary. Suppose 100 boxes
of raisins are randomly sampled, the raisins counted, and the following data are obtained.
rasins<-c(57, 44, 49, 49, 51, 54,
53, 52, 53, 45, 44, 49, 55,
47, 45, 50, 50, 39, 46, 57,
49, 60, 40, 56, 51, 58, 55,
47, 58, 53, 49, 47, 52, 51,
46, 57, 44, 48, 57, 46)
55, 46, 59, 47, 51, 53, 49, 52, 48, 46, 53,
51, 50 , 57, 45, 48, 52, 57, 54, 54, 53, 48,
55, 53, 57, 61, 56, 45, 60, 53, 52, 52, 47,
52, 53, 48, 43, 49, 46, 47, 51, 47, 54, 53,
47, 49, 48, 49, 52, 41, 50, 48, 52, 48, 53,
length(rasins)
order(rasins)
rasins[20:30]
sum(rasins[rasins>55])
which(rasins>55)
tab<-table(rasins)
tab
pie(tab)
summary(rasins)
boxplot(rasins,main="title")
boxplot(rasins,main="title")
hist(rasins,main="ABC",col='red')
?hist
hist(rasins,main="ABC",col='red',nclass=5)
w<-hist(rasins,main="ABC",col='red',nclass=5)
w
mean(rasins)
median(rasins)
sd(rasins)
mad(rasins)
59,
47,
56,
43,
47,
NONPARAMETRIC AND ROBUST STATISTICS WITH R – LAB 1
EXAMPLE 2
x<-rnorm(10,0,1)
y<-rnorm(10,5,1)
round(x,digit=2)
round(y,digit=2)
x[x>0]
y>x
z<-floor(y)
z
x^z
?floor
w<-ceiling(x^z)
w
summary(w)
table(w)
pie(table(w),col=c(1,2,3,4,5),main="rysunek nr 1",labels=c("a","b","c","d","e"),cex=2)
TASK: Repeat the above scheme for the vectors x<-runif(100,2,5), y<-runif(100,1,3)
EXAMPLE 3
x<-rt(1000,2,2)
xcdf<-ecdf(x)
plot(xcdf,lwd=4)
xpdf<-density(x)
plot(xpdf,lwd=4)
x1<-rt(1000,2,5)
xpdf1<-density(x1)
lines(xpdf1,lwd=4,col="red")
?rt
plot(xcdf,lwd=4)
text(20,0.2,"this is a cdf plot")
plot(x,x^2+2*(x1),pch=5,cex=1.2,cex.lab=1.6)
abline(2,15,lwd=2,col="red")
abline(15,2,lwd=2,col="blue")
TASK: Prepare the cdf and the pdf plots for a sample consisted of 1000 obs. from normal distribution
N(10,10).
EXAMPLE 4
z1<-rexp(100,10)
z2<-rexp(100,6)
z3<-rexp(100,4)
library(lattice)
2
NONPARAMETRIC AND ROBUST STATISTICS WITH R – LAB 1
densityplot(z1,lwd=2)
histogram(z1)
hist(z1)
h1<-hist(z1)
h1
quantile(z1, probs = seq(0, 1, 0.25))
mean(z1)
var(z2)
cor(z3,z3^2)
TASK: Estimate the density function basing on the vector z=(z1+z2+z3)/3
EXAMPLE 5
A<-matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4)
B<-matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=4,ncol=3)
B
A[,2]
A[2,]
A[2,2]
apply(A,2,mean)
apply(A,1,mean)
A*A
A%*%A
A%*%B
sss<-c()
for(i in 1: nrow(B)) sss[i]<-sum(B[i,])
sss
EXAMPLE 6
x<-c(19, 17, 17, 14, 16, 12, 15, 12, 13)
y<-c( 32, 28, 29, 24, 26, 20, 24, 20, 22)
xdev<-x-mean(x)
ydev<-y-mean(y)
xdev
ydev
proddev<-xdev*ydev
proddev
sum(proddev)/length(x)
EXAMPLE 7
xm<-mean(x)
ym<-mean(y)
results<-lm(y~x)
results
b0<-results$coef[1]
b1<-results$coef[2]
yhat<-b0+b1*x
SSE<-sum((y-yhat)^2)
SST<-sum((y-ym)^2)
SSR<-sum((yhat-ym)^2)
R2<-SSR/SST
R2
3
NONPARAMETRIC AND ROBUST STATISTICS WITH R – LAB 1
EXAMPLE 8
x<-seq(-10,10,by=0.2)
n<-length(x)
eps<-rt(n,2)
y<-2*x+1+eps
REG<-lm(y~x)
summary(REG)
plot(x,y,lwd=2,pch=3,cex.axis=1.8)
abline(REG,lwd=2,col="red")
round(REG$residuals,digit=2)
round(REG$fitted,digit=2)
RES<-REG$residuals
FIT<-REG$fitted
plot(RES,cex=3)
plot(FIT,RES,cex=3)
abline(h=0,lwd=2,col="red")
EXAMPLE 9
x1<-rnorm(20,10,10)
x2<-rnorm(20,10,10)
x1
x2
n<-length(x1)
eps<-rt(n,2)
y<-2*x1+3*x2+eps
M<-cbind(y,x1,x2)
cov(M)
cor(M)
reg<-lm(y~x1+x2)
summary(reg)
EXAMPLE 10
library(MASS)
Animals
?Animals
plot(Animals,cex=2)
plot(log(Animals),cex=2)
LAN<-log(Animals)
boxplot(LAN)
EXAMPLE 11
library(Rcmdr)
4