#EJERCICIO 1 ## literal a):Estimacion del Modelo
#Importacion del archivo
options(scipen = 999999)
library(readr)
SavingsandLoan <- read_csv("E:/Archivos para importar/SavingsandLoan.csv")
#Estimacion del modelo
library(stargazer)
modelo_saving <- lm(profit ~ revenue + offices,data= SavingsandLoan)
stargazer(modelo_saving,title = "Modelo para el margen anual de las asociaciones de
ahorro y crédito inmobiliario",type = "html",digits = 6)
Dependent variable: | |
profit | |
revenue | 0.237197*** |
(0.055559) | |
offices | -0.000249*** |
(0.000032) | |
Constant | 1.564497*** |
(0.079396) | |
Observations | 25 |
R2 | 0.865296 |
Adjusted R2 | 0.853050 |
Residual Std. Error | 0.053302 (df = 22) |
F Statistic | 70.660570*** (df = 2; 22) |
Note: | p<0.1; p<0.05; p<0.01 |
#Prueba de hipotesis
summary(modelo_saving)
Call: lm(formula = profit ~ revenue + offices, data = SavingsandLoan)
Residuals: Min 1Q Median 3Q Max -0.085090 -0.039102 -0.003341 0.030236 0.105692
Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.56449677 0.07939598 19.705 0.00000000000000182
revenue 0.23719747 0.05555937 4.269 0.000313
offices -0.00024908 0.00003205 -7.772 0.00000009508790794 *** — Signif.
codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ’ ’
1
Residual standard error: 0.0533 on 22 degrees of freedom Multiple R-squared: 0.8653, Adjusted R-squared: 0.8531 F-statistic: 70.66 on 2 and 22 DF, p-value: 0.000000000265
#grados de libertad
gl <- modelo_saving$df.residual
#Valor de Alpha
alpha <- (1 - 0.97567)
#Calculo del valor critico
qt(alpha/2,gl,lower.tail = TRUE)
[1] -2.418083
#matriz X
matriz_X <- model.matrix(modelo_saving)
matriz_P <- matriz_X %*% solve(t(matriz_X) %*% matriz_X) %*% t(matriz_X)
#Matriz M
n <- nrow(matriz_X)
matriz_M <- diag(n) - matriz_X %*% solve(t(matriz_X) %*% matriz_X) %*% t(matriz_X)
print(matriz_M[1:5, 1:5])
## 1 2 3 4 5
## 1 0.95841127 -0.03781156 -0.03048860 -0.02303563 -0.02372260
## 2 -0.03781156 0.95258677 -0.05703616 -0.06552455 -0.06569752
## 3 -0.03048860 -0.05703616 0.89953821 -0.14349084 -0.14037925
## 4 -0.02303563 -0.06552455 -0.14349084 0.77779854 -0.21541482
## 5 -0.02372260 -0.06569752 -0.14037925 -0.21541482 0.79078999
#calculo de residuos usando la matriz M
matriz_Y <- SavingsandLoan$profit
residuos_M <- matriz_M %*% matriz_Y
#Residuos del modelo
residuos_modelo <- modelo_saving$residuals
#Verificar igualdad
all.equal(as.vector(residuos_M),as.vector(residuos_modelo))
## [1] TRUE
Idempotencia <- matriz_P %*% matriz_P - matriz_P |> round(digits = 4)
print(Idempotencia[1:5, 1:5])
## 1 2 3 4
## 1 -0.00001126677 0.000011555429 -0.000011396639 0.000035630570
## 2 0.00001155543 0.000013228757 0.000036155708 0.000024545828
## 3 -0.00001139664 0.000036155708 -0.000038212212 -0.000009159513
## 4 0.00003563057 0.000024545828 -0.000009159513 0.000001462039
## 5 0.00002259693 -0.000002475223 -0.000020752651 0.000014817428
## 5
## 1 0.000022596931
## 2 -0.000002475223
## 3 -0.000020752651
## 4 0.000014817428
## 5 0.000010014908
#EJERCICIO 2 ## literal a): Estimacion del Modelo
#Importacion del archivo
options(scipen = 999999)
library(readr)
motors <- read_csv("E:/Archivos para importar/motors.csv")
#Eliminacion de filas con valores NA o infinitos
motors_clean <- na.omit(motors)
#Estimacion del modelo
library(stargazer)
modelo_fuel <- lm(milpgal ~ horspwr + weight, data= motors_clean)
stargazer(modelo_fuel,title = "Modelo para el rendimiento de vehiculos automotor",type = "html",digits = 6)
Dependent variable: | |
milpgal | |
horspwr | -0.104891*** |
(0.022330) | |
weight | -0.006614*** |
(0.000902) | |
Constant | 55.769380*** |
(1.448213) | |
Observations | 150 |
R2 | 0.723401 |
Adjusted R2 | 0.719638 |
Residual Std. Error | 3.900804 (df = 147) |
F Statistic | 192.227800*** (df = 2; 147) |
Note: | p<0.1; p<0.05; p<0.01 |
#Prueba de hipotesis
summary(modelo_fuel)
Call: lm(formula = milpgal ~ horspwr + weight, data = motors_clean)
Residuals: Min 1Q Median 3Q Max -8.0699 -2.7711 0.0203 2.0161 11.6046
Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) 55.7693834 1.4482130 38.509 < 0.0000000000000002
horspwr -0.1048910 0.0223299 -4.697 0.0000059971711
weight -0.0066143 0.0009015 -7.337 0.0000000000137 *** —
Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’
0.1 ’ ’ 1
Residual standard error: 3.901 on 147 degrees of freedom Multiple R-squared: 0.7234, Adjusted R-squared: 0.7196 F-statistic: 192.2 on 2 and 147 DF, p-value: < 0.00000000000000022
#grados de libertad
grado_libertad <- modelo_fuel$df.residual
#Valor de Alpha
alpha <- (1 - 0.96078)
#Calculo del valor critico
qt(alpha/2,grado_libertad,lower.tail = TRUE)
[1] -2.080447
#matriz X
X <- model.matrix(modelo_fuel)
P <- X %*% solve(t(X) %*% X) %*% t(X)
#Matriz M
n <- nrow(X)
M <- diag(n) - X %*% solve(t(X) %*% X) %*% t(X)
print(M[1:5, 1:5])
## 1 2 3 4 5
## 1 0.97368869 -0.01427606 -0.02392080 -0.01375068 -0.01786183
## 2 -0.01427606 0.97817053 -0.01503863 -0.01657638 -0.02068561
## 3 -0.02392080 -0.01503863 0.97794541 -0.01387917 -0.01783794
## 4 -0.01375068 -0.01657638 -0.01387917 0.98657525 -0.01638364
## 5 -0.01786183 -0.02068561 -0.01783794 -0.01638364 0.97927856
#calculo de residuos usando la matriz M
Y <- motors_clean$milpgal
residuos_M <- M %*% Y
#Residuos del modelo
residuos_modelo <- modelo_fuel$residuals
#Verificar igualdad
all.equal(as.vector(residuos_M),as.vector(residuos_modelo))
## [1] TRUE
## Literal d: Idempotencia de la matriz de proyeccion
Idempotencia <- P %*% P - P |> round(digits = 4)
print(Idempotencia [1:5, 1:5])
## 1 2 3 4 5
## 1 0.00001130583 -0.00002393522 0.00002079805 -0.00004931794 -0.00003817355
## 2 -0.00002393522 0.00002946814 0.00003863437 -0.00002362095 -0.00001438624
## 3 0.00002079805 0.00003863437 -0.00004540528 -0.00002082763 0.00003793937
## 4 -0.00004931794 -0.00002362095 -0.00002082763 0.00002474794 -0.00001635641
## 5 -0.00003817355 -0.00001438624 0.00003793937 -0.00001635641 0.00002144437