-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR-revision file.R
186 lines (159 loc) · 3.9 KB
/
R-revision file.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# importing the data from xlsx
str(Part_f)
# plotting using ggplot2
# you need the ggplot2 package installed
# if you have not installed the package
# you can install it using install.packages("ggplot2")
library(ggplot2) # calling the library
# We can a histogram and a boxplot
# histogram
names(Part_f)
ggplot(Part_f, aes(x=Income))+
geom_histogram(col="white")+
labs(title="Histogram of the Income Variable")
# boxplot
ggplot(Part_f, aes(x=Income))+
geom_boxplot()+
labs(title="Boxplot of the Income Variable")
# the boxplot reveals an outlier
# let's remove the outlier
# Drawing the histogram
#removing the outlier
n_max <- max(Part_f$Income)
n_max
x <- Part_f$Income[Part_f$Income != n_max]
range(x)
# defining the class limits
a <- seq(50000, 120000, 10000)
colors <- c("red", "blue","red","blue","red", "blue",
"red")
hist(x, breaks=a,col=colors)
range(Income)
# past exam
# question 4 (April 2022)
# part A
Z <- seq(5, 160, 5)
X <- seq(87, 56, -1)
Y <- X * Z
# i
Y[13:15]
# ii
Y[Y < 1800]
# iii
sum(Y>4800)
# Part B
#creating matrix
m <- rbind(
c(10, 4, 1),
c(5, 10, 2),
c(15, 3, 4)
)
# creating vector
v <- c(345, 450, 490)
# solving the simul equations
solve(m, v)
# Part C
# reading the data
Y <- c(61 , 47, 75, 63, 79 , 75 , 67, 47 , 71 , 84 )
Y <- as.matrix(Y, ncol=1)
Y
x <- c(19 , 19, 10 , 17 , 19, 12, 18 , 10, 15, 15 )
X <- cbind(1, x)
X
# model parameters
# solution A
solve(crossprod(X)) %*% (t(X) %*% Y)
# solution B
x_trans_x <- t(X) %*% X
x_trans_y <- t(X) %*% Y
solve(x_trans_x) %*% x_trans_y
# solution C
x_trans_x <- crossprod(X)
x_trans_y <- t(X) %*% Y
solve(x_trans_x) %*% x_trans_y
# alpha and beta
beta <- solve(x_trans_x) %*% x_trans_y
beta
# predicted values
y_hat <- X %*% beta
y_hat
# Example of data frame question
# question 5
# checking the names of the columns
names(Salaries)
head(Salaries)
# to avoid using the dollar sign, attach the data
attach(Salaries)
# Part A
# i
table(rank)
# ii
table(sex)
# Part B
# i measures of central tendency
mean(salary)
median(salary)
# for you to calculate the model..use the mfv function
# in the modeest package
library(modeest)
# the mode
mfv(salary)
# ii measures of dispersion
# standard devitiation
sd(salary)
# interquartile range
IQR(salary)
# range
# solution A
max(salary)- min(salary)
# range : solution B
diff(range(salary))
# Part C
# make_ninth
make_ninth <- function(number){
# creating an empty vector that will store the
# digits of the number
digits <- c()
# collecting the digits and appending to the list
for (i in 1:nchar(number)){
digits = append(digits, substr(number, i, i))
}
# getting the sum in odd positions
sums <- sum(as.numeric(digits)[c(1,3,5,7)])
sums
# getting the digits in even positions
even_pos_dig <- as.numeric(digits)[c(2,4,6,8)]
even_pos_dig
# doubling the digits in even positions
double_even <- 2 * even_pos_dig
double_even
# making sure that in products bigger than 9,
# the last digit has been selected
for (i in 1:4){
if (as.numeric(double_even[i]) > 9){
double_even[i] = substr(double_even[i],2,2)
}
}
double_even
# adding the double corrected even numbers
add_double_even <- sum(as.numeric(double_even))
add_double_even
# getting the total of the previous sums i.e.
# even and odd positions
total <- sums + add_double_even
total
# getting the last digit of the total using
# modulo arithmetic
last_digit_total <- total %% 10
last_digit_total
# subtracting the last digit from 10 to obtain
# the ninth digit
ninth_digit = 10 - last_digit_total
ninth_digit
# appending the ninth digit to the first eight
# numbers that was input by the user
number = number * 10 + ninth_digit
number
}
# an example
make_ninth(11233456)