Advertisement
zoro-10

COST

Apr 4th, 2024
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.40 KB | None | 0 0
  1. # Q). Using R execute the basic commands, array, list and frames
  2.  
  3. mytext<-"Good Morning"
  4. print(mytext)
  5.  
  6. #Array
  7. A<-array(c('yes','no'),dim=c(3,3,2))
  8. print(A)
  9.  
  10. #List
  11. list1<-list(c(1,2,3),4.5,5,sin)
  12. print(list1)
  13.  
  14. #Data Frames
  15. roll=c(1,2,3)
  16. name=c("abc","xyz","pqr")
  17. df=data.frame(roll,name)
  18. print(df)
  19.  
  20.  
  21. #Q). Create a matrix using R and perform the operations Addition, Subtraction,
  22. # Multiplication, Division, Transpose and Inverse.
  23.  
  24.  
  25. #Matrix 1
  26. matrix1<-matrix(c(1,2,3,4),nrow=2)
  27. print(matrix1)
  28.  
  29. #Matrix 2
  30. matrix2<-matrix(c(5,6,7,8),nrow=2)
  31. print(matrix2)
  32.  
  33. #Addition
  34. addition<-matrix1+matrix2
  35. print(addition)
  36.  
  37. #Subtraction
  38. subtraction<-matrix1-matrix2
  39. print(subtraction)
  40.  
  41. #Multiplication
  42. multiplication<-matrix1%*%matrix2
  43. print(multiplication)
  44.  
  45. #Division
  46. division<-matrix1/matrix2
  47. print(division)
  48.  
  49. #Transpose
  50. transpose<-t(matrix1)
  51. print(transpose)
  52.  
  53. #Determinant
  54. determinant<-det(matrix1)
  55. print(determinant)
  56.  
  57. #iNVERSE
  58. inverse<-solve(matrix1)
  59. print(inverse)
  60.  
  61.  
  62.  
  63.  
  64. # Q). Using R calculate the basic statistical functions: Mean, Median, Mode and Range.
  65.  
  66. #i. Data
  67. v<-c(1,2,3,4,5)
  68. print(v)
  69.  
  70. #ii. Mean
  71. result<-mean(v)
  72. print(result)
  73.  
  74. #iii. Median
  75. result<-median(v)
  76. print(result)
  77.  
  78. #iv. Mode
  79. getmode<-function(v)
  80. {
  81. uniq<-unique(v)
  82. uniq[which.max(tabulate(match(v,uniq)))]
  83. }
  84. v<-c(1,2,3,4,4,5,6,7)
  85. result<-getmode(v)
  86. print(result)
  87.  
  88. #v. Range
  89. result<-range(v)
  90. print(result)
  91.  
  92.  
  93.  
  94.  
  95. # Q). Using R import the data from excel/.CSV file and calculate the Standard  Deviation (SD), Variance, Co-variance
  96.  
  97. #i. Make an Excel File and save it with .csv extension.
  98.  
  99. # ii. Import the data from Excel/.CSV File in R.
  100. data<-read.csv(file.choose(),header=T)
  101. data
  102.  
  103. #iii. Mean
  104. mean(data$marks)
  105.  
  106. #iv. Standard Deviation (SD)
  107. sd(data$marks)
  108.  
  109. #v. Variance
  110. var(data$marks)
  111.  
  112. #vi. Co-Variance
  113. cov(data$roll,data$marks)
  114.  
  115.  
  116. # Q). Using R import the data from excel/.CSV file and draw the skewness.
  117.  
  118. #i. Install package
  119. install.packages("moments")
  120. #ii. Draw the skewness.
  121. 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)
  122. library(“moments”)
  123. skewness(time1)
  124.  
  125.  
  126.  
  127.  
  128.  
  129. # Q). Import the data from Excel/.CSV and perform the hypothetical testing.
  130.  
  131. # i. Lower tail test of population mean with known variable.
  132. # Q. Suppose the manufacturer claims that the mean lifetime of a light
  133. # bulb is more than 10,000 hours. In a sample of 30 Light bulbs, it was
  134. # found that they only last 9,900 hours on average. Assum the
  135. # population standard deviation is 120 hours. At 0.05 significant level,
  136. # can we reject the claim by the manufacture?
  137.  
  138. xbar=9900
  139. mu0=10000
  140. sigma=120
  141. n=30
  142. z=(xbar-mu0)/(sigma/sqrt(n))
  143. z
  144. alpha=0.05
  145. z.alpha=qnorm(1-alpha)
  146. z.alpha
  147.  
  148.  
  149.  
  150. # ii. Upper tail test of population mean with known variable.
  151. # Q. Suppose the food label on cookie bag states that there is at most 2
  152. # grams of saturated fat in single cookie. In a sample of 35 cookies, it is
  153. # found that the mean amount of saturated fat per cookie is 2.1 grams.
  154. # Assume that the population standard deviation is 0.25 grams. At 0.05
  155. # significance level, can we reject the claim on food label?
  156.  
  157. xbar=2.1
  158. mu0=2
  159. sigma=0.25
  160. n=35
  161. z=(xbar-mu0)/(sigma/sqrt(n))
  162. z
  163. alpha=0.05
  164. z.alpha=qnorm(1-alpha)
  165. a.alpha
  166.  
  167. # iii. Two-tail test of population mean with known variable.
  168. # Q. Suppose the mean weight of King Penguins found in an Antarctic
  169. # colony last year was 15.4 kg. In a sample of 35 Penguins same time this
  170. # year in the same colony, the mean Penguins weight is 14.6 kg. Assume
  171. # the population standard deviation is 2.5 kg. At 0.05 significance level,
  172. # can we reject the null hypothesis that the mean penguin weight does
  173. # not differ from last year?
  174.  
  175. xbar=14.6
  176. mu0=15.4
  177. sigma=2.5
  178. n=35
  179. z=(xbar-mu0)/(sigma/sqrt(n))
  180. z
  181.  
  182. alpha=0.05
  183. z.half.alpha=qnorm(1-alpha/2)
  184. c(-z.half.alpha,z.half.alpha)
  185.  
  186. # iv. Two-tail test of population mean with unknown variable.
  187. # Q. Suppose the mean weight of King Penguins found in an Antarctic
  188. # colony last year was 15.4 kg. In a sample of 35 Penguins same time this
  189. # year in the same colony, the mean Penguins weight is 14.6 kg. Assume
  190. # the population standard deviation is 2.5 kg. At 0.05 significance level,
  191. # can we reject the null hypothesis that the mean penguin weight does
  192. # not differ from last year?
  193.  
  194. xbar=14.6
  195. mu0=15.4
  196. sigma=2.5
  197. n=35
  198. z=(xbar-mu0)/(sigma/sqrt(n))
  199. z
  200.  
  201. alpha=0.05
  202. t.half.alpha=qt(1-alpha/2,df=n-1)
  203. c(-t.half.alpha,t.half.alpha)
  204.  
  205.  
  206.  
  207.  
  208. # Q). Import the data from Excel/.CSV file and perform the Chi-square Test.
  209. # i. Import Library MASS.
  210. library(MASS)
  211. print(str(Cars93))
  212. # ii. Perform the Chi-squared Test.
  213. car.data=table(Cars93$AirBags,Cars93$Type)
  214. print(car.data)
  215. print(chisq.test(car.data))
  216.  
  217.  
  218. # Q). Using R perform the binomial and normal distribution on the data.
  219. # i. Binomial Distribution
  220. # a). dbinom()
  221. x<-seq(0,50,by=1)
  222. y<-dbinom(x,50,0.5)
  223. plot(x,y)
  224.  
  225. # b). pbinom()
  226. x<-pbinom(26,51,0.5)
  227. print(x)
  228.  
  229. # c). qbinom()
  230. x<-qbinom(0.25,51,1/2)
  231. print(x)
  232.  
  233. #d). rbinom()
  234. x<-rbinom(8,150,0.4)
  235. print(x)
  236.  
  237. # ii. Normal Distribution
  238. # a). dbinom()
  239. x<-seq(-10,10,by=0.1)
  240. y<-dnorm(x,mean=2.5,sd=0.5)
  241. plot(x,y)
  242.  
  243.  
  244. # b). pbinom()
  245. x<-seq(-10,10,by=0.2)
  246. y<-pnorm(x,mean=2.5,sd=2)
  247. plot(x,y)
  248.  
  249. # c). qbinom()
  250. x<-seq(0,1,by=0.02)
  251. y<-pnorm(x,mean=2,sd=1)
  252. plot(x,y)
  253.  
  254. # d). rbinom()
  255. y<-rnorm(50)
  256. hist(y,main="Normal Distribution")
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement