Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
robertopucp
GitHub Repository: robertopucp/1eco35_2022_2
Path: blob/main/Lab1/R_lab_1.R
2714 views
1
2
################ laboratorio 1 ############################
3
## Curso: Laboratorio de R y Python ###########################
4
## @author: Roberto Mendoza
5
6
" Laboratorio 1 Python"
7
8
## ---------------------------------------------------------------------
9
10
"Types of variables"
11
12
" Correr la lineas de codugo Ctrl + enter"
13
"Codigo a texto Ctrl + Shift + c"
14
15
a2 <- 3.1614
16
print(a2)
17
typeof(a2)
18
class(a2)
19
20
21
a2 <- as.integer(a2)
22
typeof(a2)
23
class(a2)
24
25
b1 <- 10000
26
typeof(b1)
27
28
" ######## string #############"
29
30
c1 <- "My first python code"
31
print(c1)
32
typeof(c1)
33
class(c1)
34
35
c1 <- "First python code"
36
c2 <- 'at R y python Class'
37
cat(c1," : ",c2)
38
39
40
a <- paste0(c1,' : semester 2022-1')
41
42
43
print(a)
44
45
d <- 2022
46
paste0(c1,' : semester ',d, '-1')
47
48
49
#first character
50
51
cat('Fisrt letter is :', substr(c1, 1,1) )
52
53
#first word
54
cat('Fisrt word is :', substr(c1, 1,5) )
55
56
57
"cat just contatenate characters to print"
58
59
## ---------------------------------------------------------------------
60
61
"2.0 Logical variables"
62
63
a == a
64
65
1 == 1
66
67
z1 <- (1==1)
68
typeof(z1)
69
70
z1 <- as.integer(z1)
71
print(z1)
72
class(z1)
73
74
z2 <- (10 > 20)
75
as.integer(z2)
76
77
z3 <- (100 != 100)
78
z3 <- as.numeric(z3)
79
typeof(z3)
80
81
82
"TRUE <> T , FALSE <> F"
83
84
85
## ---------------------------------------------------------------------
86
87
### 3.0 Array and Matrices
88
89
# 1D numeric array
90
91
" 3.1 c() Atomic vector: simple vector data"
92
93
a <- c(1,2,3,4,"Perú")
94
print(a)
95
class(a)
96
typeof(a[1])
97
98
c2 <- c("Red", "Green", "Purple")
99
print(c2)
100
101
102
a <- c(1,2,3,4)
103
a <- append(a, 5)
104
105
" 3.2 rep( number, times) "
106
107
b <- rep(2,3) # repeat 2, 3 times
108
109
110
append(a,b)
111
112
print(mean(a))
113
print(sd(a))
114
115
rep("blue", 3)
116
117
length( rep(sample(1:100, size = 10), 3) )
118
119
120
" 3.3 seq(from , to ,by ) "
121
122
y <- seq(from = 0, to = 19, by = 2)
123
124
y <- seq(0, 19, 2)
125
126
print(y)
127
typeof(y)
128
129
y[1]
130
131
# second example
132
133
y <- seq( 1, 10)
134
print(y)
135
136
# Consecutives numbers
137
138
seq(100)
139
1:100
140
seq_len(100)
141
142
# split sequence in 50 parts
143
144
seq(100,1000, length.out = 50)
145
146
147
"3.4 Split vector"
148
149
indices <- split(seq(100), sort( seq(100) %% 3 ) )
150
indices
151
152
153
names(indices) <- c('training', 'est', 'test') ## add labels
154
print(indices)
155
156
indices$est
157
indices$test
158
159
attributes(indices) # atributos
160
161
"attribute : información de cualquier objeto en R"
162
163
" 3.5 Array: genera vectores multidimensionales R^n "
164
165
ar <- array(c(11:14, 21:24, 31:34), dim = c(2, 2, 3))
166
print(ar)
167
typeof(ar) # tipo de elementos
168
class(ar) # tipo de estrucutura del objeto
169
170
171
# array 1-dim vector
172
173
a <- array(1:20)
174
n <- length(a)
175
176
print(sd(a)*sqrt((n-1)/n)) # take care standar deviation formula
177
178
# 2D array numeric
179
180
M <- matrix(c(1,2,3,4,5,6), nrow = 2, byrow =TRUE)
181
182
print(M)
183
typeof(M)
184
dim(M)
185
186
# Matrix by colum
187
188
M <- matrix(c(1,2,3,4,5,6), nrow = 2, byrow =FALSE)
189
print(M)
190
191
cat("rows: ", dim(M)[1], '\n', "Columns: ", dim(M)[2])
192
193
dim(M)[1]
194
195
# Create a 1D NumPy array with values from 0 to 20 (exclusively) incremented by 2.5:
196
197
198
" 3.6 Matrix "
199
200
A <- matrix(c(seq(0, 9), seq( 10, 19), seq( 30, 39), seq( -20, -11), seq( 2, 20,2)), nrow = 5, byrow =TRUE)
201
A
202
203
A[2:4,] # rows selecrtion
204
205
A[,1:6] # columns selecrtion
206
207
A[,-c(2,3)] # drop columns
208
209
# Join matrix and special Matrix
210
211
M1 <- matrix(0,8,2)
212
213
print(M1)
214
215
M2 <- matrix(1,8, 4)
216
print(M2)
217
218
# horizontal stack
219
220
M3 <- cbind(M1,M2)
221
M3
222
223
M4 = matrix(c(2,2,3,4,5,1,1,5,5,9,8,2), nrow =2, byrow = TRUE)
224
print(M4)
225
226
# vertical stack
227
228
M5 <- rbind(M3,M4)
229
230
M5
231
232
## trasnpose
233
234
t(M5)
235
236
# Matrix Identity
237
238
I <- diag(8)
239
print(I)
240
241
# Reshape
242
I3 <- matrix(I, nrow = 32, ncol = 2)
243
print(I3)
244
typeof(I3)
245
246
"3.7 Factor"
247
248
university <- factor( c( rep("Licenciada",10), rep("No licenciada",100) ) )
249
attributes(university)
250
251
252
" 3. 8 List"
253
254
dis2 <- list('ATE', 'BARRANCO','BREÑA', 'CALLAO', 'CARABAYLLO','CHACLACAYO','CHORRILLOS','CIENEGUILLA'
255
,'COMAS','EL_AGUSTINO','INDEPENDENCIA')
256
dis2[[1]] # get element
257
258
259
dis1[2:5]
260
261
dis1[-1] # drop first element
262
263
# add new element
264
num <- list(13,5,5,8,9,10,5,8,13,1,20)
265
append(num, 102)
266
# add a list
267
num2 <- list(10,20,30)
268
append(num, num2)
269
270
271
cat("Suma:", sum(unlist(num)),'\n', "Minimo:", min(unlist(num)), '\n', "Maximo:", max(unlist(num)))
272
273
list1 <- list(100:130, "R", list(TRUE, FALSE))
274
275
## ---------------------------------------------------------------------
276
277
### 3.0 OLS
278
279
set.seed(756)
280
281
x1 <- runif(500)
282
x2 <- runif(500)
283
x3 <- runif(500)
284
x4 <- runif(500)
285
e <- rnorm(500)
286
287
# Poblacional regression (Data Generating Process GDP)
288
289
Y <- 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e
290
291
292
#M1 <- matrix(0,8,2)
293
294
X <- cbind(matrix(1,500), x1,x2,x3,x4)
295
head(X)
296
297
#inv(X) or solve (X)
298
299
beta <- solve(t(X) %*% X) %*% (t(X) %*% Y)
300
beta
301
302
303
304
305
306
307