#install.packages("plm")
library(plm)
## Warning: package 'plm' was built under R version 4.5.3
#install.packages("gplots")
library(gplots)
## Warning: package 'gplots' was built under R version 4.5.3
##
## ---------------------
## gplots 3.3.0 loaded:
## * Use citation('gplots') for citation info.
## * Homepage: https://talgalili.github.io/gplots/
## * Report issues: https://github.com/talgalili/gplots/issues
## * Ask questions: https://stackoverflow.com/questions/tagged/gplots
## * Suppress this message with: suppressPackageStartupMessages(library(gplots))
## ---------------------
##
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
##
## lowess
df <- data.frame(
year = c(2020,2021,2022,2023,2024,2025),
value = c(2.00, 2.22, 2.35, 2.46, 2.53, 2.60)
)
modelo_regresion <- lm(value ~ year, data = df)
summary(modelo_regresion)
##
## Call:
## lm(formula = value ~ year, data = df)
##
## Residuals:
## 1 2 3 4 5 6
## -0.071429 0.033143 0.047714 0.042286 -0.003143 -0.048571
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -231.09429 27.17159 -8.505 0.00105 **
## year 0.11543 0.01343 8.592 0.00101 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.0562 on 4 degrees of freedom
## Multiple R-squared: 0.9486, Adjusted R-squared: 0.9357
## F-statistic: 73.82 on 1 and 4 DF, p-value: 0.001008
df_pronostico <- data.frame(year=2026)
predict(modelo_regresion,df_pronostico)
## 1
## 2.764
Sueldo de los CEO y ventas de la Empresa INSTRUCCIONES: Genera el modelo de elasticidad constante y calcula el coeficiente de determinación. ¿Cuál es el pronóstico de sueldo para un CEO si la empresa vende $500 M USD?
df2 <- data.frame(
ventas = c(2.72, 7.39, 20.09, 54.60, 149.41, 403.43),
sueldo = c(375.31, 38.04, 627.50, 347.23, 448.99, 580.56)
)
modelo_elasticidad <- lm(log(sueldo) ~ log(ventas), data = df2)
summary(modelo_elasticidad)
##
## Call:
## lm(formula = log(sueldo) ~ log(ventas), data = df2)
##
## Residuals:
## 1 2 3 4 5 6
## 0.8486389 -1.6972409 0.8489452 0.0003421 -0.0012508 0.0005655
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.8221 0.9676 4.984 0.00758 **
## log(ventas) 0.2569 0.2484 1.034 0.35938
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.039 on 4 degrees of freedom
## Multiple R-squared: 0.211, Adjusted R-squared: 0.0138
## F-statistic: 1.07 on 1 and 4 DF, p-value: 0.3594
df2_pronostico <- data.frame(ventas=500)
prediccion_log <- predict(modelo_elasticidad,df2_pronostico)
prediccion <- exp(prediccion_log)
prediccion
## 1
## 613.1187
CONCLUSIÓN: La relación entre las ventas de la empresa y el sueldo de los CEO es inelástica. Por cada 1% que aumenten las ventas, el sueldo del CEO aumenta 0.25%
Método ARE para argumentos claros y convincente: A: Afirmación: Idea principal. R: Razón: Razonamiento lógico. E: Evidencia: Pruebas que apoyan la razón.
Relación de la Publicidad en las Ventas de las Principales Aerolíneas de México INSTRUCCIONES: Genera el mejor modelo de predicción. ¿Cuál es el pronóstico de ventas para cada aerolínea si en 2026 gastaran 6 M USD en publicidad?
df3 <- data.frame(
Empresa = c(
"Aeromexico","Aeromexico","Aeromexico",
"Volaris","Volaris","Volaris",
"Viva","Viva","Viva"
),
Año = c(2023,2024,2025,2023,2024,2025,2023,2024,2025),
Publicidad = c(1,2,3,1,2,3,1,2,3),
Ventas = c(12,14,16,8,10,12,4,6,8)
)
# Convertir a datos panel
df3 <- pdata.frame(
df3,
index = c("Empresa", "Año")
)
modelo_regresion2 <- lm(
Ventas ~ Publicidad,
data = df3
)
summary(modelo_regresion2)
##
## Call:
## lm(formula = Ventas ~ Publicidad, data = df3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4 -4 0 4 4
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.000 3.266 1.837 0.109
## Publicidad 2.000 1.512 1.323 0.227
##
## Residual standard error: 3.703 on 7 degrees of freedom
## Multiple R-squared: 0.2, Adjusted R-squared: 0.08571
## F-statistic: 1.75 on 1 and 7 DF, p-value: 0.2275
plotmeans(
Ventas ~ Publicidad,
data = df3
)
pooled <- plm(
Ventas ~ Publicidad,
data = df3,
model = "pooling"
)
summary(pooled)
## Pooling Model
##
## Call:
## plm(formula = Ventas ~ Publicidad, data = df3, model = "pooling")
##
## Balanced Panel: n = 3, T = 3, N = 9
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -4 -4 0 4 4
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## (Intercept) 6.0000 3.2660 1.8371 0.1088
## Publicidad 2.0000 1.5119 1.3229 0.2275
##
## Total Sum of Squares: 120
## Residual Sum of Squares: 96
## R-Squared: 0.2
## Adj. R-Squared: 0.085714
## F-statistic: 1.75 on 1 and 7 DF, p-value: 0.22745
within <- plm(
Ventas ~ Publicidad,
data = df3,
model = "within"
)
## Warning in summary.lm(object, ...): essentially perfect fit: summary may be
## unreliable
summary(within)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = Ventas ~ Publicidad, data = df3, model = "within")
##
## Balanced Panel: n = 3, T = 3, N = 9
##
## Residuals:
## Aeromexico-2023 Aeromexico-2024 Aeromexico-2025 Viva-2023 Viva-2024
## -9.0649e-16 0.0000e+00 -1.8130e-16 1.8130e-16 0.0000e+00
## Viva-2025 Volaris-2023 Volaris-2024 Volaris-2025
## -1.8130e-16 1.8130e-16 0.0000e+00 -1.8130e-16
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## Publicidad 2.000e+00 1.813e-16 1.1032e+16 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 24
## Residual Sum of Squares: 9.8608e-31
## R-Squared: 1
## Adj. R-Squared: 1
## F-statistic: 1.21694e+32 on 1 and 5 DF, p-value: < 2.22e-16
# H0: No existen efectos individuales
#
# Si p < 0.05:
# Rechazamos H0 -> NO usar Pooled
#
# Si p > 0.05:
# No rechazamos H0 -> podemos usar Pooled
pFtest(
within,
pooled
)
##
## F test for individual effects
##
## data: Ventas ~ Publicidad
## F = 2.4339e+32, df1 = 2, df2 = 5, p-value < 2.2e-16
## alternative hypothesis: significant effects
random <- plm(
Ventas ~ Publicidad,
data = df3,
model = "random"
)
## Warning in summary.lm(object, ...): essentially perfect fit: summary may be
## unreliable
## Warning in summary.lm(object, ...): essentially perfect fit: summary may be
## unreliable
summary(random)
## Oneway (individual) effect Random Effect Model
## (Swamy-Arora's transformation)
##
## Call:
## plm(formula = Ventas ~ Publicidad, data = df3, model = "random")
##
## Balanced Panel: n = 3, T = 3, N = 9
##
## Effects:
## var std.dev share
## idiosyncratic 4.207e-31 6.486e-16 0
## individual 1.600e+01 4.000e+00 1
## theta: 1
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -3.6260e-16 4.4409e-16 9.9329e-16 1.3323e-15 1.6712e-15
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## Publicidad 2.0000e+00 4.3944e-16 4.5513e+15 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 24
## Residual Sum of Squares: 9.2691e-30
## R-Squared: 1
## Adj. R-Squared: 1
## Chisq: 2.0714e+31 on 0 DF, p-value: < 2.22e-16
# H0: Los efectos aleatorios son consistentes
#
# Si p < 0.05:
# Usar efectos fijos
#
# Si p > 0.05:
# Usar efectos aleatorios
phtest(
random,
within
)
##
## Hausman Test
##
## data: Ventas ~ Publicidad
## chisq = 0, df = 1, p-value = 1
## alternative hypothesis: one model is inconsistent
df3_pronostico <- data.frame(
Empresa = c(
"Aeromexico",
"Volaris",
"Viva"
),
Año = c(2026, 2026, 2026),
Publicidad = c(6, 6, 6)
)
# Obtener coeficientes del modelo Random Effects
intercepto <- coef(random)["(Intercept)"]
pendiente <- coef(random)["Publicidad"]
# Calcular pronóstico
df3_pronostico$Ventas <- intercepto +
pendiente * df3_pronostico$Publicidad
# Mostrar pronósticos
df3_pronostico
## Empresa Año Publicidad Ventas
## 1 Aeromexico 2026 6 NA
## 2 Volaris 2026 6 NA
## 3 Viva 2026 6 NA
Relacion de la Publicidad en las Ventas de las Principales Empresas del Cuidado de la Piel en Mexico INSTRUCCIONES: Genera el mejor mordelo de prediccion. Cual es el pronostico de ventas para cada empressa si en 2026 incrementa
df4 <- data.frame(
empresa = c("Genoma Lab", "Genoma Lab", "Genoma Lab", "L 'Oreal", "L 'Oreal", "L 'Oreal", "Beiserdorf", "Beiserdorf", "Beiserdorf", "Natura", "Natura", "Natura"),
anio = c(2023,2024,2025,2023,2024,2025,2023,2024,2025,2023,2024,2025),
publicidad = c(1920, 2025, 2010, 1580, 1750, 1890, 740, 810, 870, 560, 610, 640),
ventas = c(7650, 8100, 7525, 13200, 14500, 15600, 5550, 6020, 6400, 6700, 7150, 7480)
)
# Convertir df4 en estructura de datos panel
df4 <- pdata.frame(df4, index = c("empresa", "anio"))
modelo_regresion2 <- lm(ventas ~ publicidad, data = df4)
summary(modelo_regresion2)
##
## Call:
## lm(formula = ventas ~ publicidad, data = df4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3605.5 -1928.4 -465.7 1385.9 4850.7
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4743.852 2080.470 2.280 0.0458 *
## publicidad 3.177 1.471 2.161 0.0561 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3028 on 10 degrees of freedom
## Multiple R-squared: 0.3182, Adjusted R-squared: 0.2501
## F-statistic: 4.668 on 1 and 10 DF, p-value: 0.05606
# Graficar medias de ventas según publicidad
plotmeans(ventas ~ publicidad, data = df4)
## Warning in qt((1 + p)/2, ns - 1): NaNs produced
pooled <- plm(
ventas ~ publicidad,
data = df4,
model = "pooling"
)
summary(pooled)
## Pooling Model
##
## Call:
## plm(formula = ventas ~ publicidad, data = df4, model = "pooling")
##
## Balanced Panel: n = 4, T = 3, N = 12
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -3605.55 -1928.43 -465.74 1385.87 4850.75
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## (Intercept) 4743.8524 2080.4698 2.2802 0.04577 *
## publicidad 3.1775 1.4707 2.1605 0.05606 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 134450000
## Residual Sum of Squares: 91661000
## R-Squared: 0.31824
## Adj. R-Squared: 0.25006
## F-statistic: 4.66789 on 1 and 10 DF, p-value: 0.056059
within <- plm(
ventas ~ publicidad,
data = df4,
model = "within"
)
summary(within)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = ventas ~ publicidad, data = df4, model = "within")
##
## Balanced Panel: n = 4, T = 3, N = 12
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -412.17152 -54.48479 0.64315 65.05356 356.64595
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## publicidad 7.15353 0.85758 8.3415 6.973e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 3738800
## Residual Sum of Squares: 341750
## R-Squared: 0.90859
## Adj. R-Squared: 0.85636
## F-statistic: 69.5803 on 1 and 7 DF, p-value: 6.9731e-05
pFtest(within, pooled)
##
## F test for individual effects
##
## data: ventas ~ publicidad
## F = 623.49, df1 = 3, df2 = 7, p-value = 7.358e-09
## alternative hypothesis: significant effects
random <- plm(
ventas ~ publicidad,
data = df4,
model = "random"
)
summary(random)
## Oneway (individual) effect Random Effect Model
## (Swamy-Arora's transformation)
##
## Call:
## plm(formula = ventas ~ publicidad, data = df4, model = "random")
##
## Balanced Panel: n = 4, T = 3, N = 12
##
## Effects:
## var std.dev share
## idiosyncratic 48822 221 0.003
## individual 15025933 3876 0.997
## theta: 0.9671
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -599.828 -22.082 28.425 105.481 215.134
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) -30.33108 2257.70846 -0.0134 0.9893
## publicidad 6.89640 0.84677 8.1443 3.814e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 3880200
## Residual Sum of Squares: 508340
## R-Squared: 0.86899
## Adj. R-Squared: 0.85589
## Chisq: 66.3301 on 1 DF, p-value: 3.8139e-16
phtest(random, within)
##
## Hausman Test
##
## data: ventas ~ publicidad
## chisq = 3.588, df = 1, p-value = 0.0582
## alternative hypothesis: one model is inconsistent
df4_pronostico <- data.frame(
empresa = c(
"Genoma Lab",
"L 'Oreal",
"Beiserdorf",
"Natura"
),
anio = c(2026, 2026, 2026, 2026),
publicidad = c(2200, 2000, 950, 700)
)
df4_pronostico <- data.frame(
empresa = c(
"Genoma Lab",
"L 'Oreal",
"Beiserdorf",
"Natura"
),
anio = c(2026, 2026, 2026, 2026),
publicidad = c(2200, 2000, 950, 700)
)
# Coeficientes del modelo Random Effects
intercepto <- coef(random)["(Intercept)"]
pendiente <- coef(random)["publicidad"]
# Pronóstico de ventas
df4_pronostico$ventas <- intercepto +
pendiente * df4_pronostico$publicidad
# Mostrar resultados
df4_pronostico
## empresa anio publicidad ventas
## 1 Genoma Lab 2026 2200 15141.739
## 2 L 'Oreal 2026 2000 13762.460
## 3 Beiserdorf 2026 950 6521.245
## 4 Natura 2026 700 4797.146