## 1.7 n<- 60000 d <- floor(runif(n,1,7)) total <- -2.5*length(d[d<=3]) +length(d[d==4]) + 3*length(d[d>=5]) total / n d[d<=3]<- -2.5 d[d==4]<-1 d[d>=5]<-3 mean(d) ## plot # 5.1 X <- 1:100 Y <- runif(100) V <- data.frame(X, Y) plot(V, type = "b") # 5.2 plot(V, type = "b", cex = 0.5, col = "red", pch = 12) plot(V, type = "b", cex = 1, col = "red", pch = 12,log="y") #5.3 plot(V, type = "b", cex = 0.5, col = "red", pch = 12, main = "Main title", xlab = "integer", ylab = "integer with noise") legend("bottomright", "random value", pch = 12, col = "red", pt.cex = 0.5) # 5.4 x <- seq(-2*pi,2*pi,0.1) y <- sin(x)/x plot(x,y,type="l") # 5.5 Y<-rnorm(1000,1,10) hist(Y, freq = FALSE, breaks = 20) lines(density(Y, bw = 0.5)) # 5.6 plot(ecdf(Y), do.points = FALSE, xlim = range(Y), xaxt = "n", yaxt = "n", ann = FALSE) # 5.7 v<-rbinom(100000,50,0.5); hist(v,right=FALSE,breaks = 30,freq = FALSE); curve(dnorm(x,mean=25,sd=sqrt(0.5*0.5*50)), col="green", lwd=2, add= TRUE) ## TESTS #Q 6.1 library(UsingR); data(father.son) shapiro.test(father.son$sheight - father.son$fheight) t.test(father.son$sheight - father.son$fheight,conf.level = 0.9) #Q 6.2 library(datasets); data(mtcars); m6<-mtcars$mpg[mtcars$cyl==6] m4<-mtcars[which(mtcars$cyl==4),]$mpg t.test(m4,m6,conf.level = 0.99) #Q 6.3 library(datasets); data(mtcars); mpg <- mtcars$mpg xbar <- mean(mpg) s<-sd(mpg) z<-qnorm(0.05) mu0<-xbar-z*s/sqrt(length(mpg)) #Q 6.4 library(datasets); data(mtcars); m6<-mtcars$mpg[mtcars$cyl==6] m4<-mtcars[which(mtcars$cyl==4),]$mpg t.test(m4,m6,alternative = "two.sided", var.equal = TRUE, paired = FALSE) #Q 6.5 binom.test(x=55,n=100,p=0.5,alternative = "greater",conf.level = 0.95) #Q 6.6 s<-1 n<-100 x<-round(rnorm(n,10000,s),digit=3) t.test(x,conf.level = 0.95) n<-floor(1/(0.1/(2*s*qnorm(0.975)))^2) n x<-round(rnorm(n,10000,s),digit=3) t<-t.test(x,conf.level = 0.95)$conf.int t[2]-t[1] #Q 6.7 g_tot <- 302982 b_tot <- 297960 g_bac <- g_tot*0.9104 b_bac <- b_tot*0.8602 prop.test(c(g_bac,b_bac),c(g_tot,b_tot),conf.level = 0.95) #Q 6.8 apples <- c(154, 165, 151, 171, 148, 155, 162) #check normality shapiro.test(apples) # If apples follows N then (apples-mean)/sigma follows N(0,1). # So we need to find the quantile of 175g using this normalization n_val <- (175-mean(apples))/sd(apples) #We use the the student dustributuoin with n-1 degree of freedom as we have few samples. pt(q= n_val,df = length(apples)-1, lower.tail = FALSE) 1-pnorm(175,mean(apples),sd(apples)) #6.9 f1 <- c(48.73,43.44,46.71,51.62,47.24,54.64,47.00,48.40,45.86,47.70,46.14,47.68,44.73,51.69,50.54) f2 <- c(44.89,34.31,42.74,53.36,41.98,41.64,47.24,37.86,45.89,40.88,40.85,38.60,44.38,44.52,38.26) t.test(f1,f2) #Q 6.10 hp<-c(134, 146, 104, 119, 124, 161, 107, 83, 113, 129, 97, 123) lp<-c(70, 118, 101, 85, 107, 132, 94) t.test(hp,lp, var.equal = TRUE) t.test(hp,lp, conf.level = 0.98, var.equal = FALSE) #Q11 # Test Statistic chisq.test(c(58,61,55,46),p=c(0.3,0.3,0.2,0.2))$statistic X-squared 4.189394 # Critical Value Q<- qchisq(0.95,3) Q #Q12