Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Computational Techniques in Statistics

Ronald Wesonga (Ph.D)

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

4 C = matrix(rep(0, nrow(A)*ncol(B)), nrow = nrow(A))


5

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

3 College <- c("Science","Engineering","Medicine","Economics","Law")


4

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

65 # Count how many 2 occurs in the vector rep(1:4, each = 2, times = 3)


66

67 vec <- rep(1:30, each = 4, times = 2)

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

78 # Print the numbers from 1 to 100. Print “Threes” for multiples of 3,


79 # print “Fives” for multiples of 5, and print “ThreeFives” for multiples of both 3 and 5.
80

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

91 # output <- vector()


92 #
93 # for (i in 1:100) {
94 # output[i] <- i
95 # if (i %% 3 == 0) {output[i] <- paste(output[i], "Threes")}
96 # if (i %% 5 == 0) {output[i] <- paste(output[i], "Fives")}
97 # if (output[i] == "") {output[i] <- i}
98 # }
99 #
100 # print(output)
101

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

12 numSummary(att[,c("rating", "complaints", "privileges", "learning")],


13 #groups = westost_scales$geschlecht,
14 statistics = c("mean", "sd", "se(mean)", "skewness",
15 "kurtosis", "quantiles"),
16 quantiles = c(.1,.9))
17 #******
18 # install.packages("psych", type = "binary", dependencies = TRUE)
19 library(psych)
20

21 describe(att[,c("rating", "complaints", "privileges", "learning")])


22

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

a) Define a function in R. Distinguish between an inbuilt and user-defined function.


b) How do you determine skewness in statistics? Define skewness and state its formula as well as the
algorithm to compute skewness.
c) Write an R function to determine skewness in a give vector of data.
d) Validate by testing your function on some real data.

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.

Quite Easily Done

You might also like