rm(list = ls())
Utilizar siempre minusculas, sin tildes ni caracteres especiales
Nombres de variables
Creacion de objetos
escalar1 <- 3
escalar2 <- 4
escalar1 + escalar2
## [1] 7
escalar3 <- escalar1 + escalar2
escalar3<-escalar1+escalar2
escalar4 <- escalar3 * escalar2
escalar5 <- escalar4/escalar1
escalar5
## [1] 9.333333
summary(escalar5)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 9.333 9.333 9.333 9.333 9.333 9.333
table(escalar5)
## escalar5
## 9.33333333333333
## 1
vect1 <- c(2,1.5,4) #vector numerico
vect2 <- c("Juan","Pedro","Sara")
vect3 <- c(5,6,2)
prd1 <- vect1 *escalar4
vect1[2]
## [1] 1.5
vect2[3]
## [1] "Sara"
objeto1 <- vect3[1]
objeto2 <- vect2[2:3]
objeto3 <- vect2[1:2]
objeto4 <- vect2[c(1,3)]
objeto4_f2 <- vect2[-2]
vector1 <- c(1,2,3)
vector2 <- c(4,5,6)
vector3 <- c(7,8,9)
matrizA <- cbind(vector1,vector2,vector3)
matrizA
## vector1 vector2 vector3
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
matrizB <- rbind(vector1,vector2,vector3)
matrizB
## [,1] [,2] [,3]
## vector1 1 2 3
## vector2 4 5 6
## vector3 7 8 9
base1 <- as.data.frame(matrizA)
base2 <- as.data.frame(matrizB)
base1$vector1
## [1] 1 2 3
nombre_vectores <- c("variable1","variable2","variable3")
names(base1) <- nombre_vectores
Filtros Borramos todo
rm(list = ls())
base <- mtcars
Filtros
Vehiculos con mas de 4 cilindros
base1 <- subset(base,base$cyl > 4)
## Segunda forma (matricial)
base1_f2 <- base[base$cyl>4,]
#install.packages('dplyr')
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.1
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
base1_f3 <- base %>%
filter(cyl>4)
#install.packages('readxl')
library(readxl)
## Warning: package 'readxl' was built under R version 4.5.1
#install.packages("dplyr")
library(dplyr)
#install.packages("haven")
library(haven)
## Warning: package 'haven' was built under R version 4.5.1
base <- read_excel("pib_can_anual.xlsx")
## New names:
## • `` -> `...20`
pib_can_anual <- read_excel("pib_can_anual.xlsx")
## New names:
## • `` -> `...20`
names(base)[6] <-'agricultura'
base[is.na(base)] <- 0
table(base$prov)
##
## AZUAY BOLÍVAR CAÑAR CARCHI
## 150 70 70 60
## CHIMBORAZO COTOPAXI EL ORO ESMERALDAS
## 100 70 140 71
## GALÁPAGOS GUAYAS IMBABURA LOJA
## 30 250 60 160
## LOS RÍOS MANABÍ MORONA SANTIAGO NAPO
## 130 220 120 50
## ORELLANA PASTAZA PICHINCHA SANTA ELENA
## 40 40 80 30
## SANTO DOMINGO SUCUMBÍOS TUNGURAHUA ZAMORA CHINCHIPE
## 19 70 90 90
base_santo <- base %>%
filter(dpa_prov == "23")
base_manabi <- base %>%
filter(prov== "MANABÍ")
names(base_manabi)
## [1] "year"
## [2] "prov"
## [3] "dpa_prov"
## [4] "canton"
## [5] "dpa_can"
## [6] "agricultura"
## [7] "Explotacion de minas y canteras"
## [8] "Manufactura\r\n\r\n"
## [9] "Suministro de electricidad y de agua"
## [10] "Construccion\r\n\r\n"
## [11] "comercio\r\n\r\n"
## [12] "Alojamiento y servicios de comida"
## [13] "Transporte, informacion y comunicaciones"
## [14] "Actividades financieras\r\n"
## [15] "Actividades profesionales e inmobiliarias"
## [16] "Administracion publica \r\n"
## [17] "Enseñanza\r\n\r\n"
## [18] "Salud\r\n\r\n"
## [19] "Otros servicios\r\n\r\n"
## [20] "...20"
base_manabi1 <- base_manabi %>%
select(year,prov,dpa_prov,canton,dpa_can,agricultura,'Explotacion de minas y canteras')
summary(base_manabi1$agricultura)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 80.46 4888.18 11260.08 29351.06 38616.46 234997.81
Valor limite 29351.06
base_manabi1 <- base_manabi1[,-7]
base_manabi1 <- base_manabi1 %>%
mutate(tipo_pib= ifelse(agricultura > 29351,"pib_alto","pib_bajo"))
base_manabi1 <- base_manabi1 %>%
mutate(tipo= ifelse(agricultura > 29351,1,0))
base_manabi1 <- base_manabi1 %>%
mutate(periodo= ifelse(year < 2016,"pre","post"))
base_manabi_pre <- base_manabi1 %>%
filter(periodo=="pre")
base_manabi_post <- base_manabi1 %>%
filter(periodo=="post")
b_m_pre_agg <- base_manabi_pre %>%
group_by(canton) %>%
summarise(total_agricultura_pre=sum(agricultura))
b_m_post_agg <- base_manabi_post %>%
group_by(canton) %>%
summarise(total_agricultura_post=sum(agricultura))
rm(list = ls())
base <- read.csv('base_final - base_final (1).csv')
library(dplyr)
base <- base %>%
filter(anio>=1980)
Ingreso_disponible <- base$Pib_impuestos
Ingreso_disponible <- base$pib-base$impuestos
base$ingreso_disponible <- base$pib-base$impuestos
base <- base %>%
mutate(ingreso_disponible2 = pib - impuestos)
C = B1 + B2*YD +u
options(scipen = 999)
modelo <- lm(consumo ~ ingreso_disponible,data=base)
summary(modelo)
##
## Call:
## lm(formula = consumo ~ ingreso_disponible, data = base)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4901431887 -1069569495 -195228251 1233327995 6870277166
##
## Coefficients:
## Estimate Std. Error t value
## (Intercept) 14165415645.32893 935246532.56462 15.15
## ingreso_disponible 0.68472 0.01397 49.01
## Pr(>|t|)
## (Intercept) <0.0000000000000002 ***
## ingreso_disponible <0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2450000000 on 43 degrees of freedom
## Multiple R-squared: 0.9824, Adjusted R-squared: 0.982
## F-statistic: 2402 on 1 and 43 DF, p-value: < 0.00000000000000022
base <- base %>%
mutate(ln_consumo = log(consumo),
ln_ingreso_disponible = log(ingreso_disponible))
modelo2 <- lm(ln_consumo ~ ln_ingreso_disponible, data = base)
summary(modelo2)
##
## Call:
## lm(formula = ln_consumo ~ ln_ingreso_disponible, data = base)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.113225 -0.021156 0.005125 0.031844 0.124035
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.70498 0.45862 14.62 <0.0000000000000002 ***
## ln_ingreso_disponible 0.72715 0.01852 39.26 <0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.05187 on 43 degrees of freedom
## Multiple R-squared: 0.9729, Adjusted R-squared: 0.9722
## F-statistic: 1541 on 1 and 43 DF, p-value: < 0.00000000000000022
confint(modelo)
## 2.5 % 97.5 %
## (Intercept) 12279311258.7509937 16051520031.9068737
## ingreso_disponible 0.6565423 0.7128939
confint(modelo2)
## 2.5 % 97.5 %
## (Intercept) 5.7800964 7.6298736
## ln_ingreso_disponible 0.6897913 0.7645002
rm(list = ls())
base <- read.csv("datos_ecuador - datos_ecuador.csv")
names(base)
## [1] "anio" "trim"
## [3] "RiesgoPais" "PIB_MillonesUSD"
## [5] "TasaDesempleo_Porcentaje" "IED_MillonesUSD"
modelo_desempleo <- lm(TasaDesempleo_Porcentaje ~ PIB_MillonesUSD, data=base)
modelo_desemepleo2 <- lm(base$TasaDesempleo_Porcentaje ~base$PIB_MillonesUSD)
summary(modelo_desempleo)
##
## Call:
## lm(formula = TasaDesempleo_Porcentaje ~ PIB_MillonesUSD, data = base)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.6369 -0.8933 -0.1452 0.4323 7.6118
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.37282462 0.92082867 9.093 0.000000000000595 ***
## PIB_MillonesUSD -0.00010715 0.00003556 -3.014 0.00376 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.377 on 61 degrees of freedom
## Multiple R-squared: 0.1296, Adjusted R-squared: 0.1153
## F-statistic: 9.081 on 1 and 61 DF, p-value: 0.003758
summary(modelo_desemepleo2)
##
## Call:
## lm(formula = base$TasaDesempleo_Porcentaje ~ base$PIB_MillonesUSD)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.6369 -0.8933 -0.1452 0.4323 7.6118
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.37282462 0.92082867 9.093 0.000000000000595 ***
## base$PIB_MillonesUSD -0.00010715 0.00003556 -3.014 0.00376 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.377 on 61 degrees of freedom
## Multiple R-squared: 0.1296, Adjusted R-squared: 0.1153
## F-statistic: 9.081 on 1 and 61 DF, p-value: 0.003758
names(base)
## [1] "anio" "trim"
## [3] "RiesgoPais" "PIB_MillonesUSD"
## [5] "TasaDesempleo_Porcentaje" "IED_MillonesUSD"
model_ied <- lm(IED_MillonesUSD ~ RiesgoPais, data=base)
summary(model_ied)
##
## Call:
## lm(formula = IED_MillonesUSD ~ RiesgoPais, data = base)
##
## Residuals:
## Min 1Q Median 3Q Max
## -317.37 -55.76 -16.82 54.14 279.09
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 229.89653 29.74552 7.729 0.000000000129 ***
## RiesgoPais -0.04321 0.02458 -1.758 0.0838 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 107.9 on 61 degrees of freedom
## Multiple R-squared: 0.04821, Adjusted R-squared: 0.0326
## F-statistic: 3.089 on 1 and 61 DF, p-value: 0.08382
library(dplyr)
base <- base %>%
mutate(ied_rezagado1= lag(IED_MillonesUSD,n=1),
rp_rezagado1 = lag(RiesgoPais,n=1),
rp_rezagado2 = lag(RiesgoPais,n=2))
names(base)
## [1] "anio" "trim"
## [3] "RiesgoPais" "PIB_MillonesUSD"
## [5] "TasaDesempleo_Porcentaje" "IED_MillonesUSD"
## [7] "ied_rezagado1" "rp_rezagado1"
## [9] "rp_rezagado2"
model_ied_lag1 <- lm(IED_MillonesUSD ~ rp_rezagado1, data=base)
summary(model_ied_lag1)
##
## Call:
## lm(formula = IED_MillonesUSD ~ rp_rezagado1, data = base)
##
## Residuals:
## Min 1Q Median 3Q Max
## -319.35 -55.08 -20.33 52.81 271.41
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 208.40204 30.70883 6.786 0.00000000578 ***
## rp_rezagado1 -0.02284 0.02527 -0.904 0.37
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 110.7 on 60 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.01343, Adjusted R-squared: -0.003015
## F-statistic: 0.8167 on 1 and 60 DF, p-value: 0.3698
model_ied_lag2 <- lm(IED_MillonesUSD ~ rp_rezagado2, data=base)
summary(model_ied_lag2)
##
## Call:
## lm(formula = IED_MillonesUSD ~ rp_rezagado2, data = base)
##
## Residuals:
## Min 1Q Median 3Q Max
## -320.59 -53.15 -14.34 49.29 266.92
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 214.06880 30.81112 6.948 0.00000000332 ***
## rp_rezagado2 -0.02707 0.02533 -1.069 0.29
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 111 on 59 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.01899, Adjusted R-squared: 0.002358
## F-statistic: 1.142 on 1 and 59 DF, p-value: 0.2896
Utilice la base de datos “datos_ecuador.csv”, para el periodo entre 2015 y 2023, Corra el modelo desempleo = B1 + B2*Ln(PIB) +u. Indique el valor de B2. (Ingrese su valor con 2 decimales sin redondear)
rm(list = ls())
base <- read.csv("datos_ecuador - datos_ecuador.csv")
Filtro
base1 <- base %>%
filter(anio> 2015 & anio < 2023) %>%
mutate(ln_pib=log(PIB_MillonesUSD))
Modelo
names(base1)
## [1] "anio" "trim"
## [3] "RiesgoPais" "PIB_MillonesUSD"
## [5] "TasaDesempleo_Porcentaje" "IED_MillonesUSD"
## [7] "ln_pib"
modelo1 <- lm(TasaDesempleo_Porcentaje ~ ln_pib,data = base1)
summary(modelo1)
##
## Call:
## lm(formula = TasaDesempleo_Porcentaje ~ ln_pib, data = base1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.4957 -0.9472 -0.3826 0.3515 6.4448
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 105.698 42.899 2.464 0.0207 *
## ln_pib -9.759 4.205 -2.321 0.0284 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.559 on 26 degrees of freedom
## Multiple R-squared: 0.1716, Adjusted R-squared: 0.1398
## F-statistic: 5.387 on 1 and 26 DF, p-value: 0.0284
B2= -9.75
Utilice la base de datos “datos_ecuador.csv”, Para el periodo deshde 2015 hasta 2023, corra el modelo desempleo = B1 + B2*PIB +u. Indique el valor de B2. (Ingrese su valor con 2 decimales sin redondear)
rm(list = ls())
base <- read.csv("datos_ecuador - datos_ecuador.csv")
base2 <- base %>%
filter(anio>= 2015 & anio <= 2023)
modelo2 <- lm(TasaDesempleo_Porcentaje~PIB_MillonesUSD, data = base2)
summary(modelo2)
##
## Call:
## lm(formula = TasaDesempleo_Porcentaje ~ PIB_MillonesUSD, data = base2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.8356 -0.8271 -0.1033 0.1037 6.8451
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.6811501 2.7428198 4.623 0.0000526 ***
## PIB_MillonesUSD -0.0002485 0.0001002 -2.481 0.0182 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.451 on 34 degrees of freedom
## Multiple R-squared: 0.1533, Adjusted R-squared: 0.1284
## F-statistic: 6.155 on 1 and 34 DF, p-value: 0.01821
MANEJO DE ENCUESTAS
rm(list = ls())
#Librerias
library(dplyr)
#install.packages('srvyr')
library(srvyr)
## Warning: package 'srvyr' was built under R version 4.5.1
##
## Adjuntando el paquete: 'srvyr'
## The following object is masked from 'package:stats':
##
## filter
#install.packages('rio')
library(rio)
## Warning: package 'rio' was built under R version 4.5.1
#install.packages('import')
library(import)
## Warning: package 'import' was built under R version 4.5.1
## The import package should not be attached.
## Use "colon syntax" instead, e.g. import::from, or import:::from.
personas <- import("1_BDD_ENS2018_f1_personas (3).dta")
table(personas$dcronica_2)
##
## 0 1
## 5591 2210
dci <- 2210/(5591+2210)
dci*100
## [1] 28.3297
dm <- personas %>%
as_survey_design(ids=upm, #unidad primaria de muestreo
strata=estrato, #estrato
weights = fexp) #factor de expansion
options(survey.lonely.psu = 'certainty') #forzando a que las observaciones sean unicas e individuales
prev_nac_dci2 <- dm %>%
summarise(survey_mean(dcronica_2, vartype =c('se','cv'),na.rm=T),
n_muestra=sum(!is.na(dcronica_2))) %>%
mutate(dominio="Nacional") %>%
select(dominio, porcentaje=coef, se=`_se`, cv=`_cv`, n_muestra) %>%
mutate(porcentaje=round((porcentaje*100),digits = 1))
rm(list = ls())
library(dplyr)
library(srvyr)
library(rio)
#installed.packages('haven')
library(haven)
df <- read.csv2("enemdu_persona_2025_II_trimestre.csv")
df2 <- read.csv("enemdu_persona_2025_II_trimestre.csv",sep = ';')
dm <- df %>%
as_survey_design(ids=upm, #unidad primaria de muestreo
strata=estrato, #estrato
weights = fexp) #factor de expansion
TASA DE DESEMPLEO NACIONAL
tasa_desempleo_nac <- dm %>%
filter(p03>=15) %>%
summarise(
tasa_desempleo = survey_ratio(
numerator=( condact == 7 | condact==8),
denominator=(condact %in% 1:8),
vartype = c('se','ci')
)
) %>%
mutate(tasa_desempleo = round((tasa_desempleo*100),digits = 1))
TASA DE EMPLEO ADECUADO
tasa_empleo_adec <- dm %>%
filter(p03>=15) %>%
summarise(
tasa_empleo_adecuado = survey_ratio(
numerator=( condact == 1),
denominator=(condact %in% 1:8),
vartype = c('se','ci')
)
) %>%
mutate(tasa_empleo_adecuado = round((tasa_empleo_adecuado*100),digits = 1))
TASA DE subempleo
tasa_subempleo_nac <- dm %>%
filter(p03>=15) %>%
summarise(
tasa_subempleo = survey_ratio(
numerator=( condact == 2 | condact==3),
denominator=(condact %in% 1:8),
vartype = c('se','ci')
)
) %>%
mutate(tasa_subempleo = round((tasa_subempleo*100),digits = 1))
TASA DE EMPLEO no remunerado
tasa_empleo_norem <- dm %>%
filter(p03>=15) %>%
summarise(
tasa_empleo_norem = survey_ratio(
numerator=( condact == 5),
denominator=(condact %in% 1:8),
vartype = c('se','ci')
)
) %>%
mutate(tasa_empleo_norem = round((tasa_empleo_norem*100),digits = 1))