1. Reading and commenting code

Read the following code, and write comments on each line explaining what is going on.

x <- c(1,2,3,4,5,6,7) # Assigning the values into x.
y <- mean(x) # Assigning the mean of x into y. 
x2 <- x - y  # Creating a variable x2 and assigning values of (x-y) into x2. 
a1 <- runif(100) # 100 Random Uniform values between 0 and 1 are assigned into a1.  
a2 <- rnorm(100) # 100 Ramdom Normalized values are assigned into a2.
a3 <- a1 * a2 # Multiplying a1 and a2.   
a.tab <- cbind(a1,a2,a3)  # Combining values of a1, a2 and a3 by rows and columns and assigning them into a.tab.
pairs(a.tab) # Scaterred Plot is plotted for a.tab.

cor(a.tab) # Correlation of a.tab is calculated. 
##             a1         a2          a3
## a1  1.00000000 -0.0855807 -0.07202879
## a2 -0.08558070  1.0000000  0.84806883
## a3 -0.07202879  0.8480688  1.00000000

2. Assignment of values

Determine what happens to both a and b when you do the following. Write an answer in words.

Answer: Step 1: Value of 100 is assigned into a. Step 2: Value of a which is 100, is assigned into b. Step 3: Value of a is reassigned by 200.

 a <- c(1,2,3,4,5)
 b <- a
 b[3] <- 10

Answer: Step 1: List of values are assigned into a. Step 2: Values of a are assigned into b. Step 3: 3rd Value in the list of b is replaced by the value 10.

3. Computing Means of data

x1 <- runif(1000)
x1.mean <-sum(x1)/length(x1)
print(x1.mean)
## [1] 0.4963223
x1.hmean <-1 / mean(1/x1)

print(x1.hmean)
## [1] 0.06338116
x1.gmean <-exp(mean(log(x1)))

print(x1.gmean)
## [1] 0.3585372
hist(x1)
abline(v=x1.mean,lwd=2, col="pink")
text(x1.mean,100,"Arithmetic mean")
abline(v=x1.hmean,lwd=2,col="green")
text(x1.hmean,100,"Harmonic mean")
abline(v=x1.gmean,lwd=2,col="yellow")
text(x1.gmean,100,"Geometric mean")

x2  <- runif(1000) * 2

x2.mean <-sum(x2)/length(x2)
print(x2.mean)
## [1] 1.03944
x2.hmean <-1 / mean(1/x2)

print(x2.hmean)
## [1] 0.2388568
x2.gmean <-exp(mean(log(x2)))

print(x2.gmean)
## [1] 0.7633021
hist(x2)
abline(v=x2.mean,lwd=3, col="pink")
text(x2.mean,100,"Arithmetic mean")
abline(v=x2.hmean,lwd=3,col="green")
text(x2.hmean,100,"Harmonic mean")
abline(v=x2.gmean,lwd=3,col="yellow")
text(x2.gmean,100,"Geometric mean")

x3 <- runif(1000) + 2

x3.mean <-sum(x3)/length(x3)
print(x3.mean)
## [1] 2.490458
x3.hmean <-1 / mean(1/x3)

print(x3.hmean)
## [1] 2.456784
x3.gmean <-exp(mean(log(x3)))

print(x3.gmean)
## [1] 2.473614
hist(x3)
abline(v=x3.mean,lwd=2, col="pink")
text(x3.mean,100,"Arithmetic mean")
abline(v=x3.hmean,lwd=2,col="green")
text(x3.hmean,100,"Harmonic mean")
abline(v=x3.gmean,lwd=2,col="yellow")
text(x3.gmean,100,"Geometric mean")

x4 <- 5/(runif(1000)+.04)
x4 <- runif(1000) + 2

x4.mean <-sum(x4)/length(x4)
print(x4.mean)
## [1] 2.499185
x4.hmean <-1 / mean(1/x4)

print(x4.hmean)
## [1] 2.465803
x4.gmean <-exp(mean(log(x4)))

print(x4.gmean)
## [1] 2.482523
hist(x4)
abline(v=x4.mean,lwd=3, col="pink")
text(x4.mean,100,"Arithmetic mean")
abline(v=x4.hmean,lwd=3,col="green")
text(x4.hmean,100,"Harmonic mean")
abline(v=x4.gmean,lwd=3,col="yellow")
text(x4.gmean,100,"Geometric mean")

x5 <- exp(rnorm(1000))
x5 <- runif(1000) + 2

x5.mean <-sum(x5)/length(x5)
print(x5.mean)
## [1] 2.501198
x5.hmean <-1 / mean(1/x5)

print(x5.hmean)
## [1] 2.466684
x5.gmean <-exp(mean(log(x5)))

print(x5.gmean)
## [1] 2.483972
hist(x5)
abline(v=x5.mean,lwd=3, col="pink")
text(x5.mean,100,"Arithmetic mean")
abline(v=x5.hmean,lwd=3,col="green")
text(x5.hmean,100,"Harmonic mean")
abline(v=x5.gmean,lwd=3,col="yellow")
text(x5.gmean,100,"Geometric mean")

Answer: In the graph, we can see that the values shifts towards the right when a positive value is added to the parameter.

4. Normalizing data.

x1 <- 1:20
par(mfrow=c(1,2))
plot(x1, main="before",sub=paste("mean=", mean(x1),
       "sd=",round(sd(x1),4)),cex=.5)
x1.norm <- (x1-mean(x1))/sd(x1)
plot(x1.norm, main="after",
     sub=paste("mean=",mean(x1.norm),"sd=",sd(x1.norm)),cex=.5)

mean(x1.norm) 
## [1] 0
sd(x1.norm)   
## [1] 1
x2 <- runif(100) + rnorm(100)*3
par(mfrow=c(1,2))
plot(x2, main="before",sub=paste("mean=", mean(x2),
       "sd=",round(sd(x2),4)),cex=.5)
x2.norm <- (x2-mean(x2))/sd(x2)
plot(x2.norm, main="after",
     sub=paste("mean=",mean(x2.norm),"sd=",sd(x2.norm)),cex=.5)

mean(x2.norm) 
## [1] -2.545056e-17
sd(x2.norm)
## [1] 1
x3 <- seq(1,100,3)
par(mfrow=c(1,2))
plot(x3, main="before",sub=paste("mean=", mean(x3),
       "sd=",round(sd(x3),4)),cex=.5)
x3.norm <- (x3-mean(x3))/sd(x3)
plot(x3.norm, main="after",
     sub=paste("mean=",mean(x3.norm),"sd=",sd(x3.norm)),cex=.5)

mean(x3.norm) 
## [1] 0
sd(x3.norm)
## [1] 1
x4 <- c(runif(10),300)
par(mfrow=c(1,2))
plot(x4, main="before",sub=paste("mean=", mean(x4),
       "sd=",round(sd(x4),4)),cex=.5)
x4.norm <- (x4-mean(x4))/sd(x4)
plot(x4.norm, main="after",
     sub=paste("mean=",mean(x4.norm),"sd=",sd(x4.norm)),cex=.5)

mean(x4.norm) 
## [1] 5.085894e-18
sd(x4.norm)
## [1] 1
x5 <- 1/(runif(50)*3)
par(mfrow=c(1,2))
plot(x5, main="before",sub=paste("mean=", mean(x5),
       "sd=",round(sd(x5),4)),cex=.5)
x5.norm <- (x5-mean(x5))/sd(x5)
plot(x5.norm, main="after",
     sub=paste("mean=",mean(x5.norm),"sd=",sd(x5.norm)),cex=.5)

mean(x5.norm) 
## [1] 3.938581e-17
sd(x5.norm)
## [1] 1

In the above graphs, only x1 and x3 have the Mean and Standard Deviation as 0 and 1 respectively.