Path: blob/main/Trabajo_grupal/WG1/Grupo_7_r.R
2714 views
## Ejercicio 112y <- runif(20,0,500)34for (i in y) {5if (i <0 & i <= 100) {6next7i <- i^(1/2)89} else if (i > 100 & i <= 300) {10i <- i - 51112} else {13i <- 5014}15y = i16}1718print (y)1920212223## Ejercicio 22425## Para la matriz26272829sample <- runif(5000,0,500)3031M <- matrix(sample, 100, 50)3233if(! is.matrix(M)) stop("M must be a matrix")3435calculator <- function (M,h,z) {36h <- apply(M, 2, min)37z <- apply(M, 2, max)3839result = (M - h) / (z - h)40return(result)41}4243calculator(M, h, z) [1]444546# Para el vector4748y <- runif(100,0,500)4950if(! is.vector(y)) stop("y must be a vector")5152calculator1 <- function (y,k,l) {53k <- apply(y, 1, min)54l <- apply(y, 1, max)5556result = (y - k) / (l - k)57return(result)58}5960calculator1(y, k, l) [1]6162636465## Ejercicio 4666768x1 <- runif(800)69x2 <- runif(800)70x3 <- runif(800)71x4 <- runif(800)72x5 <- runif(800)73x6 <- runif(800)74x7 <- runif(800)75e <- rnorm(800)7677z <- rnorm(800)7879c=180Y <- c + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 1.2*x5 + 0.5*x6 + 1.5*x7 + e81X <- cbind(matrix(1,800), x1,x2,x3,x4,x5,x6,x7)8283ols <- function(M, Y , standar = T, Pvalue = T , instrumento = NULL, index = NULL){848586if (standar & Pvalue & is.null(instrumento) & is.null(index)){8788beta <- solve(t(M) %*% M) %*% (t(M) %*% Y)8990y_est <- M %*% beta91n <- dim(M)[1]92k <- dim(M)[2] - 193df <- n- k94sigma <- sum(sapply(Y - y_est , function(x) x ^ 2))/ df9596Var <- sigma*solve(t(M) %*% M)97sd <- sapply(diag(Var) , sqrt)9899t.est <- abs(beta/sd)100pvalue <-2*pt(t.est, df = df, lower.tail = FALSE) ## pt : t - student densidad101102table <- data.frame(OLS= beta,103standar.error = sd, P.value = pvalue)104105106}107108109if ( !is.null(instrumento) & !is.null(index) ){110111beta <- solve(t(M) %*% M) %*% (t(M) %*% Y)112113index <- index + 1114115Z <- X116Z[,index] <- z117118beta_x <- solve(t(Z) %*% Z) %*% (t(Z) %*% X[,index])119120x_est <- Z %*% beta_x121X[,index] <- x_est122123beta_iv <- solve(t(X) %*% X) %*% (t(X) %*% Y)124125table <- data.frame(OLS= beta,126OLS.IV = beta_iv)127128129130return(table)131132133