Professional Documents
Culture Documents
Computational Techniques in Statistics: Exercise 1
Computational Techniques in Statistics: Exercise 1
19 Feb 24
Abstract
R: Controlling, Looping, conditioning and Functioning.
Exercise 1
You are required to multiply two matrices A = matrix(sample(c(0, 1), 100, replace = T ), nrow = 10) and
B = matrix(rnorm(100), nrow = 10) using the R for() looping function rather than C = A% ∗ %B
1 A <- matrix(sample(c(0, 1), 100, replace = TRUE), nrow = 10)
2 B <- matrix(round(rnorm(100, 0,1), 3), nrow = 10)
3
6 for(i in 1:nrow(A)){
7 for(j in 1:ncol(B)){
8 for(k in 1:nrow(B)){
9 C[i,j] <- C[i,j] + A[i,k]*B[k, j]
10 }
11 }
12 }
13
14 print(C)
Vectorized Looping
1 # Character Vector
2
5 for(i in 1:5){
6 print(College[i])
7 }
8
9 # for(i in seq_along(College)){
10 # print(College[i])
11 # }
12
13 # Numeric Vector
14
1
15 x <- seq(0, 34, 4)
16 n = length(x)
17 a = 1
18 for(i in 1:n)
19 {
20 x[i] = x[i] + i
21 a[i+1] = 2*a[i]
22 }
23 print(cbind(x=x, a=a))
24
25 s = 1
26 repeat
27 {
28 s <- s+3;
29 print (s);
30 if (s > 10)
31 break;
32 }
33
34 s = 0
35 repeat {
36 s = s+1
37 print(s)
38 if (s == 4) {
39 print ("Finally End Here!");
40 break
41 }
42 }
43
44 scores = c(80,61,72,83,94,85)
45 vec = 0
46 n = length(scores)
47
48 for (i in 1:n) {
49 vec <- vec+scores[i]
50 }
51 meen <- vec/n
52 # print(meen)
53
54 # Get the first 10 Fibonacci numbers. The Fibonacci numbers form a sequence,
55 # such that each number is the sum of the two preceding ones, starting from 0 and 1.
56
57 f <- numeric(10)
58 f[1] <- 0
59 f[2] <- 1
60 for (i in 3:10) {
61 f[i] <- f[i - 2] + f[i - 1]
62 }
63 # print(f)
64
2
68
69 count <- 0
70 for (i in seq_along(vec)){
71 if (vec[i] == 2){
72 count <- count + 1
73 }
74 }
75
76 # print(count)
77
81 ec <- c()
82 for (n in 1:100){
83 if (n %% 3 == 0){ vec[n] <- "Threes" }
84 else if (n %% 5 == 0){ vec[n] <- "Fives" }
85 else if (n %% 3 == 0 & n %% 5 == 0){ vec[n] <- "ThreeFives" }
86 else { vec[n] <- n }
87 }
88
89 # print(vec)
90
102 # To discontinue one cycle and continue to the next use next.
103 num <- 0:6
104 for (i in num) {
105 if (i == 3 | i ==6) {
106 next
107 }
108 print(i)
109 }
3
Descriptive Statistics
• summary() returns descriptive statistics for all variables in a dataset. For numeric variables, the
minimum, maximum, quartiles, median, and mean values are returned, for factors the frequencies of
the factor levels. In addition, the number of missing values for both variable types is displayed.
Exercise 1
Import the inbuilt data called attitude under the package MASS.
1) Using the inbuilt function summary(), obtain descriptive statistics for all variables in a dataset
2) Using the function numSummary() in the package RcmdrMisc, obtain obtain descriptive statistics
for the variables: “rating”, “complaints”, “privileges” and “learning”. Display descriptive statistics,
including: “mean”, “sd”, “se(mean)”, “skewness”, “kurtosis”, “quantiles”.
3) Using the function describe() in the package psych, obtain obtain descriptive statistics for the variables:
“rating”, “complaints”, “privileges” and “learning”. Hint to install do: install.packages(“psych”, type =
“binary”, dependencies = TRUE).
4) Using the function descriptives() in the package jmv, obtain obtain descriptive statistics for the
variables: “rating”, “complaints”, “privileges” and “learning”.
5) Using the function corrMatrix() in the package jmv, obtain obtain the correlational statistics for the
variables: “raises”, “critical” and “advance”.
1 #library(MASS)
2
3 #******
4 att <- datasets::attitude
5 summary(att)
6 names(att)
7
8 #******
9 # install.packages("RcmdrMisc")
10 library(RcmdrMisc)
11
23 #******
24 # install.packages("jmv")
25 library(jmv)
26 descriptives(att[,c("raises", "critical", "advance")])
27 corrMatrix(att[,c("raises", "critical", "advance")], ci = TRUE)
4
Research Question 1
Research Question 2
You are required to write an R function to multiply two matrices A = matrix(sample(c(0, 1), 100, replace =
T ), nrow = 10) and B = matrix(rnorm(100), nrow = 10) using the R for() looping function. Test your
function using real data.