Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
robertopucp
GitHub Repository: robertopucp/1eco35_2022_2
Path: blob/main/Lab2/Lab_R_script.R
2710 views
1
################ laboratorio 2 ############################
2
## Curso: Laboratorio de R y Python ###########################
3
## @author: Roberto Mendoza
4
5
" If statement"
6
# -------------------------------------------------------
7
8
9
y <- runif(10,-10,10) # runif( n: cantidad de elementos, inicio , final)
10
11
if (mean(y) > 0) {
12
dummy <- 1
13
} else {
14
dummy <- 0
15
}
16
17
print(dummy)
18
19
"Nested If statement"
20
21
# v <- 2
22
# v <- NA
23
# v <- "String"
24
v <- TRUE
25
26
27
if ( is.numeric(v) ){
28
cat(v, " es un numero entero (no missing)")
29
} else if ( is.na(v) ) {
30
31
cat(v, " es un missing")
32
33
} else if ( is.character(v) ){
34
35
cat(v, " es un string")
36
37
} else if ( is.logical(v) ){
38
39
cat(v, " es un Boolean")
40
41
} else {
42
43
print("Sin resultado")
44
45
}
46
47
48
" Loops "
49
# -------------------------------------------------------
50
51
52
# saving
53
S <- 1000
54
55
# Periods
56
n <- 10
57
58
# interes rate
59
i <- 0.025
60
61
62
year = 1
63
while (year < n){
64
S <- S*(1+i)
65
year <- year + 1
66
cat( "periodo ", year, ": ", S,"\n")
67
}
68
69
#### While + If statement
70
71
w <- 10
72
73
while (w > 7 & w <= 15){
74
coin <- round( runif(1) )
75
coin
76
if (coin == 1) {
77
w <- w + 2
78
} else {
79
w <- w - 10
80
}
81
82
}
83
print(w)
84
85
#### For
86
87
ages<- c(21, 23, 25, 24, 20)
88
89
for (age in ages) {
90
91
print(age+10 )
92
93
}
94
95
96
###For and Next, break
97
98
for (i in 1:50) {
99
if(i %in% 15:20) { # Ignora los primeros 20 elementos
100
next
101
print(i + 1000)
102
}
103
cat("Ejecutanto",i,"\n")
104
}
105
106
107
### For and Next, break
108
109
110
for (j in 1:100){
111
print(j)
112
113
if(j > 20){
114
break
115
}
116
117
}
118
119
120
### While + break
121
122
w <- 10
123
124
while (TRUE){
125
coin <- round( runif(1) ) # redondear al entero más cercano
126
if (coin == 1) {
127
break
128
} else {
129
w <- w - 10
130
print(w)
131
}
132
133
}
134
135
136
137
" Function "
138
# -------------------------------------------------------
139
140
## First function ##
141
142
calculator <- function(x,y,z)
143
{
144
result = x*y*z
145
return(result)
146
}
147
148
calculator( 158, 38, 10 )
149
150
calculator( 158, 38)
151
152
## return multiple
153
154
calculator_square <- function(x,y){
155
x2 <- x * x
156
y2 <- y * y
157
158
result <- x2 * y2
159
return(list(x2,x2,paste0("La multiplicación del cuadrado es:", result)) )
160
}
161
162
calculator_square(3, 4)[1]
163
calculator_square(3, 4)[[1]] # para ontener el elemento simple
164
165
166
## IF statement and return
167
calculator_square_2 <- function(x,y){
168
x2 <- x * x
169
y2 <- y * y
170
171
result <- x2 * y2
172
173
if (200 >= result) {
174
return( cat( "Large number. Get only the result variable: ", result) )
175
} else {
176
return( print( "Too large number. Do not return variables!") )
177
178
}
179
180
}
181
182
183
calculator_square_2(300, 4)
184
185
## Función y tipo de variables ##
186
187
calculator_base_5 <- function( x, y){
188
if(! is.numeric(x)) stop("x must be a numero")
189
if(! is.double(y)) stop("y must be a double")
190
191
result = x*y
192
193
return(as.double(result))
194
195
}
196
197
calculator_base_5( NA, 3.0)
198
199
calculator_base_5( 1, 3.0)
200
201
## Function y valore predeterminados de parámetros
202
203
transpose <- function(M, est = TRUE, z = NULL ){
204
if(! is.matrix(M)) stop("x must be a matrix")
205
206
M <- t(M)
207
208
if (est & is.null(z)){
209
for (j in seq(dim(M)[2])) {
210
v <- M[,j]
211
M[,j] <- ( v - mean(v) )/sd(v)
212
}
213
214
} else if (! is.null(z) ) {
215
M <- M*z
216
217
}
218
219
return(M)
220
221
}
222
223
A <- matrix(c(seq(0, 9), seq( 10, 19), seq( 30, 39), seq( -20, -11), seq( 2, 20,2)), nrow = 5, byrow =TRUE)
224
225
print(transpose(A))
226
227
print(transpose(A, est = FALSE, z = 2))
228
229
## Equivalent *args de Python en R
230
231
caso1 <- function(...) return(sum(...))
232
233
caso1(2,4,5)
234
235
236
237
caso2 <- function(...) {
238
239
return(prod(...))
240
241
242
}
243
244
caso2(sample(1:50, size = 5))
245
246
### Try and error
247
248
249
a = "2"
250
251
252
tryCatch(a/7,
253
254
error = function(e) {
255
256
cat("Correción del tipo de variable:" , as.integer(a) / 7)
257
258
}
259
)
260
261
262
263
"Loop Replacement"
264
# -------------------------------------------------------
265
266
#Check function
267
str(apply)
268
269
270
271
set.seed(756) # semilla aleatoria
272
273
"Loop Replacement using list"
274
X <- runif(10)
275
print(X)
276
277
lapply(X, function(square) {square^2})
278
279
280
x <- seq(10)
281
lapply(X, rep, times = 5) # recordar rep(numero , times = numero de repeticiones)
282
283
x1 <- runif(500)
284
x2 <- runif(500)
285
x3 <- runif(500)
286
x4 <- runif(500)
287
X <- cbind(matrix(1,500),x1,x2,x3,x4)
288
289
# Matrix a lista por cada columna
290
291
lapply(seq_len(ncol(X)),function(x) X[ , x])
292
293
"sapply es similar a lappy pero no genera una lista sino valores numéricos"
294
295
X <- runif(10)
296
print(X)
297
298
lapply(X, function(square) {square^2})
299
sapply(X, function(square) {square^2}) # vector canonico simple
300
301
x <- seq(10)
302
lapply(X, rep, times = 5)
303
sapply(X, rep, times = 5) # matrix output
304
305
"Apply aplicado al caso de array multidimensionales (DataFrame , Matrix) "
306
str(apply)
307
str(rnorm)
308
309
x <- matrix(rnorm(500), 100, 5) # 100 filas , 5 columnas
310
311
apply(x, 2, mean, simplify = F) # MARGIN == 2 para columnas
312
apply(x, 2, mean, simplify = TRUE)
313
314
apply(x, 1, sd) # MARGIN == 1 para filas
315
316
apply(x, 2, min)
317
318
apply(x, 1, max)
319
320
"estandarización usando apply"
321
322
apply(x, 2, function(i){
323
( i - mean(i) ) / sd(i)
324
} )
325
326
"mapply - apply multivariado"
327
328
est <- function(mean, sd, x){
329
(x - mean)/sd
330
}
331
332
str(mapply)
333
334
mapply(est, mean = 1:5, sd = seq(0.1,0.5,0.1), MoreArgs = list(x = x))
335
336
337
" Time "
338
339
# Start the clock!
340
ptm <- proc.time()
341
342
for (i in 1:10000000) {
343
if(i %in% 15:20) { # Ignora los primeros 20 elementos
344
next
345
print(i + 1000)
346
}
347
#cat("Ejecutanto",i,"\n")
348
}
349
350
proc.time() - ptm
351
352
# Parallel procesing
353
354
n <- 1000000
355
x1 <- runif(n)
356
x2 <- runif(n)
357
x3 <- runif(n)
358
x4 <- runif(n)
359
X <- cbind(matrix(1,n),x1,x2,x3,x4)
360
361
ptm <- proc.time()
362
363
364
apply(X, 2, function(i){
365
( i - mean(i) ) / sd(i)
366
} )
367
368
proc.time() - ptm
369
370
"parallel es un paquete instalado por default"
371
372
install.packages("parallel")
373
# library("parallel")
374
375
376
no_of_cores = detectCores()
377
print(no_of_cores)
378
379
"
380
parLapply(cl, x, FUN, ...)
381
parApply(cl = NULL, X, MARGIN, FUN, ...)
382
parSapply(cl = NULL, X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) "
383
384
library("parallel")
385
386
387
#####################################################3
388
389
390
ptm <- proc.time()
391
lapply( X, function(i){
392
( i - mean(i) ) / sd(i)
393
} )
394
395
proc.time() - ptm
396
397
398
### Parallel process
399
400
401
cl = makeCluster(no_of_cores )
402
403
ptm <- proc.time()
404
parLapply(cl, X, function(i){
405
( i - mean(i) ) / sd(i)
406
} )
407
408
proc.time() - ptm
409
410
######################### Apply #########################
411
412
ptm <- proc.time()
413
apply(X, 2, function(i){
414
( i - mean(i) ) / sd(i)
415
} )
416
417
proc.time() - ptm
418
419
#########################################################
420
421
422
cl = makeCluster(no_of_cores )
423
424
ptm <- proc.time()
425
apply(X, 2, function(i){
426
( i - mean(i) ) / sd(i)
427
} )
428
429
proc.time() - ptm
430
431
stopCluster(cl)
432
433
434
" DataFrame"
435
# -------------------------------------------------------
436
437
df <- data.frame(face = c("ace", "two", "six"),
438
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3))
439
str(df) # similar describe en Stata
440
441
df <- data.frame(face = c("ace", "two", "six"),
442
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3), stringsAsFactors = T) # read FACTOR (string)
443
str(df)
444
445
446
"Function by OLS estimation, standar error, IV, P-value "
447
448
# -------------------------------------------------------
449
450
set.seed(756) # permite que los numeros aleatorios no cambien al correr los códigos
451
x1 <- runif(500)
452
x2 <- runif(500)
453
x3 <- runif(500)
454
x4 <- runif(500)
455
e <- rnorm(500)
456
457
#Isntrumento
458
459
z <- rnorm(500)
460
461
# Poblacional regression (Data Generating Process GDP)
462
463
Y <- 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e
464
X <- cbind(matrix(1,500), x1,x2,x3,x4)
465
466
ols <- function(M, Y , standar = T, Pvalue = T , instrumento = NULL, index = NULL){
467
468
469
if (standar & Pvalue & is.null(instrumento) & is.null(index)){
470
471
beta <- solve(t(M) %*% M) %*% (t(M) %*% Y)
472
473
y_est <- M %*% beta ## Y estimado
474
n <- dim(M)[1] # filas
475
k <- dim(M)[2] - 1 # varaibles sin contar el intercepto}
476
df <- n- k ## grados de libertad
477
sigma <- sum(sapply(Y - y_est , function(x) x ^ 2))/ df
478
479
Var <- sigma*solve(t(M) %*% M)
480
sd <- sapply(diag(Var) , sqrt) ## raíz cuadrado a los datos de la diagonal principal de Var
481
482
t.est <- abs(beta/sd)
483
pvalue <-2*pt(t.est, df = df, lower.tail = FALSE) ## pt : t - student densidad
484
485
table <- data.frame(OLS = beta,
486
standar.error = sd, P.value = pvalue)
487
488
489
}
490
491
492
if ( !is.null(instrumento) & !is.null(index) ){
493
494
beta <- solve(t(M) %*% M) %*% (t(M) %*% Y)
495
496
index <- index + 1
497
498
Z <- X
499
Z[,index] <- z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables
500
501
beta_x <- solve(t(Z) %*% Z) %*% (t(Z) %*% X[,index])
502
503
x_est <- Z %*% beta_x
504
X[,index] <- x_est ## se reemplaza la variable x endógena por su estimado
505
506
beta_iv <- solve(t(X) %*% X) %*% (t(X) %*% Y)
507
508
table <- data.frame(OLS= beta,
509
OLS.IV = beta_iv)
510
511
}
512
513
return(table)
514
}
515
516
517
ols(X,Y)
518
519
ols(X,Y,instrumento = z, index = 1)
520
521
522
a = c(1,2)
523
typeof(a)
524
class(a)
525
is.vector(a)
526
527