1. Basic calculations

1+2 + 2*3 - 2^2 + exp(2)/2
## [1] 8.694528
2*pi
## [1] 6.283185

2. Create scalor, vector, and matrix

\[ a=3, b=(1,2,3,4),c=(4,5,6,7), d = {\begin{pmatrix} 1 & 4\\ 2 & 5\\ 3 & 6\\ 4 & 7\\ \end{pmatrix}}, e= \begin{pmatrix} 1 & 2 & 3 & 4\\ 4 & 5 & 6 & 7\\ \end{pmatrix} \]

a <- 3 
print(a)
## [1] 3
b <- c(1,2,3,4)
print(b)
## [1] 1 2 3 4
print(b[1])
## [1] 1
c <- 4:7
d <- cbind(b,c)
print(d)
##      b c
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## [4,] 4 7
e <- rbind(b,c)
print(e)
##   [,1] [,2] [,3] [,4]
## b    1    2    3    4
## c    4    5    6    7
print(e[2,2])
## c 
## 5
print(e[2,])
## [1] 4 5 6 7

\[ 2b+1=(2\times 1+1,2\times 2+1,2\times 3+1,2\times 4 + 1)\]

print(2*b+1)
## [1] 3 5 7 9

\[ b + c = (1+4,2+5,3+6,4+7) \]

print(b+c)
## [1]  5  7  9 11

\[ 2e=\begin{pmatrix} 2 \times 1 +5& 2 \times 2+5 & 2 \times 3+5 & 2 \times 4+5\\ 2 \times 4+5 & 2 \times 5+5 & 2 \times 6+5 & 2 \times 7+5\\ \end{pmatrix} \]

print(2*e+5)
##   [,1] [,2] [,3] [,4]
## b    7    9   11   13
## c   13   15   17   19

\[ d'd = {\begin{pmatrix} 1 & 2 & 3 & 4\\ 4 & 5 & 6 & 7\\ \end{pmatrix}} ' {\begin{pmatrix} 1 & 4\\ 2 & 5\\ 3 & 6\\ 4 & 7\\ \end{pmatrix}} \]

print(t(d)%*%d)
##    b   c
## b 30  60
## c 60 126

3. if else

a<-3
if(a ==3){
  print("a==3")
}else{
  print("a!=3")
}
## [1] "a==3"

4. for loop

\(x=1+2+3+...+10, x=?\)

x<-0
for(i in 1:10){
  x=x+i
}
print(x)
## [1] 55

Example:

Chapter 3.3, finite population sampling problem: population={1,2,3,4}, sample size n=2, sampling with replacement

sample.mat<-cbind(c(1,1,1,2,2,3,1,2,3,4),c(2,3,4,3,4,4,1,2,3,4))
delta<-c(0.1,0.1,0.4,0.4)

i=1
sample.prob<-tau.hat<-s2<-Vhat<-NULL
for(i in 1:10){
    tmp1<-sample.mat[i,1]
    tmp2<-sample.mat[i,2]
    if(tmp1!=tmp2){
        sample.prob[i]<-2*delta[tmp1]*delta[tmp2]
    }else{
        sample.prob[i]<-delta[tmp1]*delta[tmp2]
    }
    tau.hat[i]<-(tmp1/delta[tmp1]+tmp2/delta[tmp2])/2
    s2[i]<-(tmp1/delta[tmp1]-tau.hat[i])^2+(tmp2/delta[tmp2]-tau.hat[i])^2
    Vhat[i]<-s2[i]/2
}

print(cbind(sample.mat,sample.prob,tau.hat,s2,Vhat))
##           sample.prob tau.hat     s2    Vhat
##  [1,] 1 2        0.02   15.00 50.000 25.0000
##  [2,] 1 3        0.08    8.75  3.125  1.5625
##  [3,] 1 4        0.08   10.00  0.000  0.0000
##  [4,] 2 3        0.08   13.75 78.125 39.0625
##  [5,] 2 4        0.08   15.00 50.000 25.0000
##  [6,] 3 4        0.32    8.75  3.125  1.5625
##  [7,] 1 1        0.01   10.00  0.000  0.0000
##  [8,] 2 2        0.01   20.00  0.000  0.0000
##  [9,] 3 3        0.16    7.50  0.000  0.0000
## [10,] 4 4        0.16   10.00  0.000  0.0000

Without for loop

tmp1.v<-sample.mat[,1]
tmp2.v<-sample.mat[,2]
sample.prob.v<-delta[tmp1.v]*delta[tmp2.v]*c(2,2,2,2,2,2,1,1,1,1)
tau.hat.v<-(tmp1.v/delta[tmp1.v]+tmp2.v/delta[tmp2.v])/2
s2.v<-(tmp1.v/delta[tmp1.v]-tau.hat.v)^2+(tmp2.v/delta[tmp2.v]-tau.hat.v)^2
Vhat.v<-s2.v/2

print(cbind(sample.mat,sample.prob.v,tau.hat.v,s2.v,Vhat.v))
##           sample.prob.v tau.hat.v   s2.v  Vhat.v
##  [1,] 1 2          0.02     15.00 50.000 25.0000
##  [2,] 1 3          0.08      8.75  3.125  1.5625
##  [3,] 1 4          0.08     10.00  0.000  0.0000
##  [4,] 2 3          0.08     13.75 78.125 39.0625
##  [5,] 2 4          0.08     15.00 50.000 25.0000
##  [6,] 3 4          0.32      8.75  3.125  1.5625
##  [7,] 1 1          0.01     10.00  0.000  0.0000
##  [8,] 2 2          0.01     20.00  0.000  0.0000
##  [9,] 3 3          0.16      7.50  0.000  0.0000
## [10,] 4 4          0.16     10.00  0.000  0.0000

The expectation of \(\hat{\tau}\)

E.tau.hat<-sum(sample.prob*tau.hat)
print(E.tau.hat)
## [1] 10

The expectation of \(\hat{V}(\hat{\tau})\)

E.Vhat<-sum(sample.prob*Vhat)
print(E.Vhat)
## [1] 6.25