Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Q). Using R execute the basic commands, array, list and frames
- mytext<-"Good Morning"
- print(mytext)
- #Array
- A<-array(c('yes','no'),dim=c(3,3,2))
- print(A)
- #List
- list1<-list(c(1,2,3),4.5,5,sin)
- print(list1)
- #Data Frames
- roll=c(1,2,3)
- name=c("abc","xyz","pqr")
- df=data.frame(roll,name)
- print(df)
- #Q). Create a matrix using R and perform the operations Addition, Subtraction,
- # Multiplication, Division, Transpose and Inverse.
- #Matrix 1
- matrix1<-matrix(c(1,2,3,4),nrow=2)
- print(matrix1)
- #Matrix 2
- matrix2<-matrix(c(5,6,7,8),nrow=2)
- print(matrix2)
- #Addition
- addition<-matrix1+matrix2
- print(addition)
- #Subtraction
- subtraction<-matrix1-matrix2
- print(subtraction)
- #Multiplication
- multiplication<-matrix1%*%matrix2
- print(multiplication)
- #Division
- division<-matrix1/matrix2
- print(division)
- #Transpose
- transpose<-t(matrix1)
- print(transpose)
- #Determinant
- determinant<-det(matrix1)
- print(determinant)
- #iNVERSE
- inverse<-solve(matrix1)
- print(inverse)
- # Q). Using R calculate the basic statistical functions: Mean, Median, Mode and Range.
- #i. Data
- v<-c(1,2,3,4,5)
- print(v)
- #ii. Mean
- result<-mean(v)
- print(result)
- #iii. Median
- result<-median(v)
- print(result)
- #iv. Mode
- getmode<-function(v)
- {
- uniq<-unique(v)
- uniq[which.max(tabulate(match(v,uniq)))]
- }
- v<-c(1,2,3,4,4,5,6,7)
- result<-getmode(v)
- print(result)
- #v. Range
- result<-range(v)
- print(result)
- # Q). Using R import the data from excel/.CSV file and calculate the Standard Deviation (SD), Variance, Co-variance
- #i. Make an Excel File and save it with .csv extension.
- # ii. Import the data from Excel/.CSV File in R.
- data<-read.csv(file.choose(),header=T)
- data
- #iii. Mean
- mean(data$marks)
- #iv. Standard Deviation (SD)
- sd(data$marks)
- #v. Variance
- var(data$marks)
- #vi. Co-Variance
- cov(data$roll,data$marks)
- # Q). Using R import the data from excel/.CSV file and draw the skewness.
- #i. Install package
- install.packages("moments")
- #ii. Draw the skewness.
- time1<-c(19.09,19.55,17.89,17.73,25.15,27.27,25.24,21.65,20.92,22,61.15,71,22.04,22.60,24.25)
- library(“moments”)
- skewness(time1)
- # Q). Import the data from Excel/.CSV and perform the hypothetical testing.
- # i. Lower tail test of population mean with known variable.
- # Q. Suppose the manufacturer claims that the mean lifetime of a light
- # bulb is more than 10,000 hours. In a sample of 30 Light bulbs, it was
- # found that they only last 9,900 hours on average. Assum the
- # population standard deviation is 120 hours. At 0.05 significant level,
- # can we reject the claim by the manufacture?
- xbar=9900
- mu0=10000
- sigma=120
- n=30
- z=(xbar-mu0)/(sigma/sqrt(n))
- z
- alpha=0.05
- z.alpha=qnorm(1-alpha)
- z.alpha
- # ii. Upper tail test of population mean with known variable.
- # Q. Suppose the food label on cookie bag states that there is at most 2
- # grams of saturated fat in single cookie. In a sample of 35 cookies, it is
- # found that the mean amount of saturated fat per cookie is 2.1 grams.
- # Assume that the population standard deviation is 0.25 grams. At 0.05
- # significance level, can we reject the claim on food label?
- xbar=2.1
- mu0=2
- sigma=0.25
- n=35
- z=(xbar-mu0)/(sigma/sqrt(n))
- z
- alpha=0.05
- z.alpha=qnorm(1-alpha)
- a.alpha
- # iii. Two-tail test of population mean with known variable.
- # Q. Suppose the mean weight of King Penguins found in an Antarctic
- # colony last year was 15.4 kg. In a sample of 35 Penguins same time this
- # year in the same colony, the mean Penguins weight is 14.6 kg. Assume
- # the population standard deviation is 2.5 kg. At 0.05 significance level,
- # can we reject the null hypothesis that the mean penguin weight does
- # not differ from last year?
- xbar=14.6
- mu0=15.4
- sigma=2.5
- n=35
- z=(xbar-mu0)/(sigma/sqrt(n))
- z
- alpha=0.05
- z.half.alpha=qnorm(1-alpha/2)
- c(-z.half.alpha,z.half.alpha)
- # iv. Two-tail test of population mean with unknown variable.
- # Q. Suppose the mean weight of King Penguins found in an Antarctic
- # colony last year was 15.4 kg. In a sample of 35 Penguins same time this
- # year in the same colony, the mean Penguins weight is 14.6 kg. Assume
- # the population standard deviation is 2.5 kg. At 0.05 significance level,
- # can we reject the null hypothesis that the mean penguin weight does
- # not differ from last year?
- xbar=14.6
- mu0=15.4
- sigma=2.5
- n=35
- z=(xbar-mu0)/(sigma/sqrt(n))
- z
- alpha=0.05
- t.half.alpha=qt(1-alpha/2,df=n-1)
- c(-t.half.alpha,t.half.alpha)
- # Q). Import the data from Excel/.CSV file and perform the Chi-square Test.
- # i. Import Library MASS.
- library(MASS)
- print(str(Cars93))
- # ii. Perform the Chi-squared Test.
- car.data=table(Cars93$AirBags,Cars93$Type)
- print(car.data)
- print(chisq.test(car.data))
- # Q). Using R perform the binomial and normal distribution on the data.
- # i. Binomial Distribution
- # a). dbinom()
- x<-seq(0,50,by=1)
- y<-dbinom(x,50,0.5)
- plot(x,y)
- # b). pbinom()
- x<-pbinom(26,51,0.5)
- print(x)
- # c). qbinom()
- x<-qbinom(0.25,51,1/2)
- print(x)
- #d). rbinom()
- x<-rbinom(8,150,0.4)
- print(x)
- # ii. Normal Distribution
- # a). dbinom()
- x<-seq(-10,10,by=0.1)
- y<-dnorm(x,mean=2.5,sd=0.5)
- plot(x,y)
- # b). pbinom()
- x<-seq(-10,10,by=0.2)
- y<-pnorm(x,mean=2.5,sd=2)
- plot(x,y)
- # c). qbinom()
- x<-seq(0,1,by=0.02)
- y<-pnorm(x,mean=2,sd=1)
- plot(x,y)
- # d). rbinom()
- y<-rnorm(50)
- hist(y,main="Normal Distribution")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement