Importar paquetes y llamar librerías

#install.packages("readxl")
library(readxl)
#install.packages("plm")
library(plm)
#install.packages("gplots")
library(gplots)
library(tidyverse)
library(plm)
library(gplots)
#install.packages("WDI")
library(WDI)
#install.packages("wbstats")
library(wbstats)

Parte 1. Patentes

#¿Qué nivel de ventas estimaría el modelo si las empresas obtuvieran todas las patentes que solicitaron?

df1 <- read.csv("/Users/carlalievanoespinosa/Desktop/business analytics/8vo semestre/df1.csv")


df1 <- pdata.frame(df1, index = c("cusip", "year"))

#Prueba de heterogeneidad
plotmeans(sales ~ patentsg, data = df1)
## Warning in qt((1 + p)/2, ns - 1): NaNs produced
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped

# OPCIÓN 1 - MODELO DE REGRESIÓN AGRUPADA (POOLED)
pooled <- plm(sales ~ patentsg,
              data = df1,
              model = "pooling")

summary(pooled)
## Pooling Model
## 
## Call:
## plm(formula = sales ~ patentsg, data = df1, model = "pooling")
## 
## Unbalanced Panel: n = 226, T = 9-10, N = 2257
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -16270.2   -562.7   -488.0   -265.7  40900.9 
## 
## Coefficients:
##              Estimate Std. Error t-value  Pr(>|t|)    
## (Intercept) 521.29460   68.93369  7.5623 5.729e-14 ***
## patentsg     25.70451    0.82306 31.2305 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    3.1e+10
## Residual Sum of Squares: 2.164e+10
## R-Squared:      0.30193
## Adj. R-Squared: 0.30162
## F-statistic: 975.345 on 1 and 2255 DF, p-value: < 2.22e-16
# OPCIÓN 2 - MODELO DE EFECTOS FIJOS (WITHIN)
within <- plm(sales ~ patentsg,
              data = df1,
              model = "within")

summary(within)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = sales ~ patentsg, data = df1, model = "within")
## 
## Unbalanced Panel: n = 226, T = 9-10, N = 2257
## 
## Residuals:
##        Min.     1st Qu.      Median     3rd Qu.        Max. 
## -16152.9468    -73.4763     -8.4409     58.4344  20901.6815 
## 
## Coefficients:
##          Estimate Std. Error t-value  Pr(>|t|)    
## patentsg -26.2818     1.6203  -16.22 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    4308200000
## Residual Sum of Squares: 3813900000
## R-Squared:      0.11474
## Adj. R-Squared: 0.016178
## F-statistic: 263.099 on 1 and 2030 DF, p-value: < 2.22e-16
# PRUEBA F
# INTERPRETACIÓN:
# Si p-value < 0.05 -> NO usar POOLED -> usar EFECTOS FIJOS
# Si p-value > 0.05 -> usar POOLED

pFtest(within, pooled)
## 
##  F test for individual effects
## 
## data:  sales ~ patentsg
## F = 42.17, df1 = 225, df2 = 2030, p-value < 2.2e-16
## alternative hypothesis: significant effects
# OPCIÓN 3 - MODELO DE EFECTOS ALEATORIOS (RANDOM)
random <- plm(sales ~ patentsg,
              data = df1,
              model = "random")

summary(random)
## Oneway (individual) effect Random Effect Model 
##    (Swamy-Arora's transformation)
## 
## Call:
## plm(formula = sales ~ patentsg, data = df1, model = "random")
## 
## Unbalanced Panel: n = 226, T = 9-10, N = 2257
## 
## Effects:
##                   var std.dev share
## idiosyncratic 1878776    1371 0.215
## individual    6870102    2621 0.785
## theta:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.8283  0.8368  0.8368  0.8367  0.8368  0.8368 
## 
## Residuals:
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## -1.29e+04 -2.43e+02 -2.17e+02  1.32e-01 -1.14e+02  2.50e+04 
## 
## Coefficients:
##              Estimate Std. Error z-value  Pr(>|z|)    
## (Intercept) 1435.9200   194.9184  7.3668 1.748e-13 ***
## patentsg      -8.0209     1.4298 -5.6098 2.025e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    5018700000
## Residual Sum of Squares: 4949700000
## R-Squared:      0.013745
## Adj. R-Squared: 0.013307
## Chisq: 31.4704 on 1 DF, p-value: 2.025e-08
# PRUEBA DE HAUSMAN
# INTERPRETACIÓN:
# Si p-value < 0.05 -> usar EFECTOS FIJOS
# Si p-value > 0.05 -> usar EFECTOS ALEATORIOS

phtest(random, within)
## 
##  Hausman Test
## 
## data:  sales ~ patentsg
## chisq = 573.88, df = 1, p-value < 2.2e-16
## alternative hypothesis: one model is inconsistent
#Como el p-value de la prueba de Hausman es menor a 0.05, el modelo de Efectos Fijos es más adecuado que el modelo de Efectos Aleatorios.

# PRONÓSTICO 2022
# ESCENARIO: todas las patentes solicitadas son concedidas

df1_pronostico <- subset(
  df1,
  year == 2021,
  select = c("cusip", "patents")
)

# El pronóstico será para 2022
df1_pronostico$year <- 2022

# Suponemos que todas las patentes solicitadas son concedidas
df1_pronostico$patentsg <- df1_pronostico$patents

# Convertir a datos panel
df1_pronostico <- pdata.frame(
  df1_pronostico,
  index = c("cusip", "year")
)

# Obtener la pendiente del modelo de efectos fijos
pendiente <- coef(within)["patentsg"]

# Obtener los efectos fijos de cada empresa
efectos <- fixef(within)

# Pronóstico de ventas para 2022
df1_pronostico$sales_pronostico <-
  efectos[as.character(df1_pronostico$cusip)] +
  pendiente * df1_pronostico$patentsg

# Ver resultados
df1_pronostico
##              cusip patents year patentsg sales_pronostico
## 800-2022       800       5 2022        5      1660.685634
## 4626-2022     4626       0 2022        0       384.265351
## 4671-2022     4671       0 2022        0        66.894517
## 7500-2022     7500       0 2022        0        47.125439
## 7603-2022     7603       0 2022        0       101.684539
## 20753-2022   20753       1 2022        1        42.481798
## 21367-2022   21367       0 2022        0        56.309978
## 23519-2022   23519       1 2022        1       652.642434
## 29069-2022   29069       1 2022        1        17.743640
## 38213-2022   38213       0 2022        0       123.191776
## 54303-2022   54303       0 2022        0      1844.655373
## 67131-2022   67131       0 2022        0       235.645395
## 67383-2022   67383       0 2022        0       299.252631
## 74077-2022   74077       1 2022        1      6798.849451
## 77491-2022   77491       0 2022        0       113.756360
## 87509-2022   87509       7 2022        7      6292.362170
## 87779-2022   87779       3 2022        3       457.153399
## 105655-2022 105655       0 2022        0        47.184539
## 118745-2022 118745       1 2022        1       516.055373
## 125761-2022 125761       0 2022        0       231.278070
## 126149-2022 126149       1 2022        1      3491.507784
## 134429-2022 134429       1 2022        1      1930.142653
## 147195-2022 147195       0 2022        0       166.027193
## 149123-2022 149123      14 2022       14     12182.810625
## 158663-2022 158663       0 2022        0       722.137171
## 165339-2022 165339       0 2022        0       897.238158
## 171196-2022 171196       7 2022        7     12897.967652
## 172172-2022 172172       1 2022        1      1315.931249
## 189873-2022 189873       0 2022        0       173.669079
## 200291-2022 200291       0 2022        0       199.873574
## 202363-2022 202363       0 2022        0       233.832675
## 212363-2022 212363       3 2022        3      2094.265131
## 212813-2022 212813       0 2022        0       245.547149
## 229669-2022 229669       0 2022        0       262.865351
## 235773-2022 235773       0 2022        0       524.781798
## 235811-2022 235811       0 2022        0      2408.924999
## 244199-2022 244199      10 2022       10      5112.341554
## 252741-2022 252741      15 2022       15      2363.626753
## 266867-2022 266867       1 2022        1       110.548136
## 268039-2022 268039       2 2022        2       254.821710
## 277461-2022 277461      97 2022       97     12439.008104
## 278058-2022 278058      14 2022       14      4560.370062
## 286065-2022 286065       0 2022        0       115.073574
## 296659-2022 296659       1 2022        1       195.655373
## 296695-2022 296695       1 2022        1        74.159101
## 303711-2022 303711       1 2022        1       615.927193
## 316549-2022 316549       1 2022        1       446.063596
## 345370-2022 345370      16 2022       16     35889.007888
## 345838-2022 345838       1 2022        1       -12.325439
## 350244-2022 350244       5 2022        5      1660.610525
## 351604-2022 351604       2 2022        2       553.741666
## 361428-2022 361428       9 2022        9      2240.602629
## 361556-2022 361556       0 2022        0       122.379057
## 361606-2022 361606       0 2022        0       228.042653
## 362360-2022 362360       1 2022        1        25.584539
## 368298-2022 368298       0 2022        0       186.260855
## 368514-2022 368514       0 2022        0        45.353618
## 369032-2022 369032       0 2022        0        94.069079
## 369154-2022 369154       0 2022        0       212.324452
## 369550-2022 369550       6 2022        6      4229.157235
## 369604-2022 369604     168 2022      168     36012.958740
## 369856-2022 369856      11 2022       11      5979.767433
## 370622-2022 370622       0 2022        0       515.475329
## 370838-2022 370838       1 2022        1      2231.523572
## 372298-2022 372298       0 2022        0        28.469079
## 375046-2022 375046       0 2022        0       243.104496
## 375766-2022 375766       3 2022        3      2273.914034
## 377352-2022 377352       0 2022        0       293.018969
## 383492-2022 383492       9 2022        9      3216.747804
## 383550-2022 383550       0 2022        0       180.325439
## 383883-2022 383883      18 2022       18      6404.327408
## 384109-2022 384109       0 2022        0       229.165351
## 390568-2022 390568       1 2022        1       122.635417
## 402784-2022 402784       0 2022        0       231.711732
## 404245-2022 404245       0 2022        0       212.365351
## 413342-2022 413342       0 2022        0       624.113487
## 413875-2022 413875       1 2022        1      1867.807345
## 415864-2022 415864       2 2022        2       821.368092
## 421596-2022 421596       1 2022        1       208.555373
## 422191-2022 422191       0 2022        0        34.769079
## 423002-2022 423002       0 2022        0        37.412719
## 423236-2022 423236       0 2022        0       186.304496
## 428399-2022 428399       0 2022        0       105.291776
## 428875-2022 428875       0 2022        0        41.640899
## 429812-2022 429812       0 2022        0       151.242653
## 439272-2022 439272       4 2022        4       641.040899
## 449290-2022 449290       0 2022        0       216.883552
## 449680-2022 449680       1 2022        1        29.777778
## 451542-2022 451542       0 2022        0       340.128180
## 451650-2022 451650       0 2022        0       356.295285
## 456866-2022 456866       7 2022        7      2935.822038
## 457186-2022 457186       0 2022        0       210.472710
## 457776-2022 457776       0 2022        0        42.084539
## 459101-2022 459101       0 2022        0       170.648136
## 459200-2022 459200      51 2022       51     29825.100199
## 459506-2022 459506      58 2022       58       283.102741
## 459578-2022 459578      14 2022       14      6991.219077
## 459884-2022 459884       2 2022        2      1897.650438
## 460043-2022 460043       0 2022        0       888.940899
## 461135-2022 461135       0 2022        0        99.650877
## 462218-2022 462218       0 2022        0       139.483552
## 465632-2022 465632       0 2022        0       979.762170
## 479169-2022 479169       0 2022        0        75.950877
## 481070-2022 481070       0 2022        0       283.224452
## 481088-2022 481088       0 2022        0       209.540899
## 481196-2022 481196       4 2022        4       761.968092
## 486872-2022 486872       0 2022        0       134.519956
## 487836-2022 487836       0 2022        0      1496.809978
## 489170-2022 489170       1 2022        1       607.352412
## 493503-2022 493503       0 2022        0       128.019956
## 494368-2022 494368      13 2022       13      2308.310525
## 495620-2022 495620       0 2022        0       136.460855
## 501026-2022 501026       0 2022        0       131.612719
## 501206-2022 501206       0 2022        0       156.891776
## 503624-2022 503624       0 2022        0        42.684539
## 505336-2022 505336       0 2022        0       147.125439
## 513696-2022 513696       0 2022        0       320.365351
## 513847-2022 513847       0 2022        0       224.053618
## 524660-2022 524660       0 2022        0       259.783552
## 530000-2022 530000       2 2022        2      1513.555920
## 538021-2022 538021       7 2022        7      4974.496160
## 539821-2022 539821       3 2022        3      4468.179385
## 540137-2022 540137       1 2022        1       211.127193
## 540210-2022 540210       0 2022        0        47.197259
## 541381-2022 541381       1 2022        1        68.535417
## 543213-2022 543213       1 2022        1       251.030921
## 551120-2022 551120       0 2022        0        68.522697
## 551137-2022 551137       0 2022        0        35.525439
## 552618-2022 552618       0 2022        0       232.014474
## 562706-2022 562706       0 2022        0        56.722697
## 574055-2022 574055       0 2022        0       583.010745
## 574599-2022 574599       0 2022        0       660.700767
## 575379-2022 575379       0 2022        0       497.701754
## 576680-2022 576680       0 2022        0        40.212719
## 580033-2022 580033       0 2022        0      2054.363596
## 580169-2022 580169       5 2022        5      5328.389143
## 580628-2022 580628       6 2022        6      1840.127740
## 585055-2022 585055       6 2022        6       507.966118
## 589331-2022 589331      39 2022       39      5373.586397
## 597715-2022 597715       4 2022        4      1109.974122
## 601073-2022 601073       1 2022        1       163.691776
## 608030-2022 608030       0 2022        0       678.748136
## 608183-2022 608183       0 2022        0       292.642653
## 620076-2022 620076      24 2022       24      6128.870497
## 629853-2022 629853       8 2022        8      1345.347476
## 637742-2022 637742       1 2022        1       352.299013
## 670148-2022 670148       0 2022        0       151.512719
## 670250-2022 670250       0 2022        0        33.684539
## 680665-2022 680665      28 2022       28      3407.471269
## 690207-2022 690207       1 2022        1       218.619956
## 690734-2022 690734      40 2022       40      2451.877411
## 690768-2022 690768      20 2022       20      5788.378613
## 704562-2022 704562       3 2022        3       559.116228
## 707389-2022 707389       0 2022        0        26.484539
## 717081-2022 717081      41 2022       41      3324.344515
## 718320-2022 718320       0 2022        0       245.353618
## 724479-2022 724479      17 2022       17      1909.265458
## 727346-2022 727346       0 2022        0        83.725439
## 727491-2022 727491       0 2022        0       110.735417
## 736245-2022 736245       0 2022        0       522.791776
## 737407-2022 737407       0 2022        0        80.089035
## 739732-2022 739732       0 2022        0       119.766338
## 739868-2022 739868       0 2022        0        43.612719
## 740512-2022 740512       0 2022        0       244.653618
## 746252-2022 746252       2 2022        2       589.929934
## 746299-2022 746299       0 2022        0       115.335417
## 749720-2022 749720       2 2022        2         7.671820
## 749738-2022 749738       0 2022        0       228.614474
## 750633-2022 750633       1 2022        1         7.387281
## 754688-2022 754688       0 2022        0       108.950877
## 754713-2022 754713       0 2022        0        75.507237
## 755111-2022 755111       2 2022        2      5447.822693
## 756040-2022 756040       0 2022        0        27.884539
## 758114-2022 758114       0 2022        0        40.928180
## 760354-2022 760354       0 2022        0       284.279057
## 760881-2022 760881       0 2022        0       362.183552
## 760898-2022 760898       0 2022        0        25.140899
## 761406-2022 761406       0 2022        0       612.725439
## 766481-2022 766481       0 2022        0       401.958114
## 767329-2022 767329       0 2022        0         8.456360
## 768024-2022 768024       0 2022        0       146.476316
## 770196-2022 770196       0 2022        0       180.732675
## 770519-2022 770519       5 2022        5      1825.995173
## 770553-2022 770553       1 2022        1       675.715241
## 775133-2022 775133       1 2022        1       128.904496
## 775371-2022 775371       7 2022        7      3055.682234
## 776338-2022 776338       0 2022        0       180.604496
## 776678-2022 776678       0 2022        0       488.790814
## 776755-2022 776755       1 2022        1       696.190569
## 784015-2022 784015      12 2022       12      2386.170174
## 784626-2022 784626       0 2022        0       401.357127
## 794099-2022 794099       0 2022        0       181.242653
## 799850-2022 799850       1 2022        1       831.519516
## 809367-2022 809367       6 2022        6       472.635417
## 809877-2022 809877       4 2022        4      1982.813267
## 810640-2022 810640       0 2022        0       998.168859
## 817698-2022 817698       0 2022        0        58.835417
## 817732-2022 817732       0 2022        0        18.384539
## 820208-2022 820208       1 2022        1        79.909978
## 822440-2022 822440       0 2022        0        69.366338
## 826520-2022 826520       0 2022        0        93.476316
## 828675-2022 828675       1 2022        1       185.055373
## 831865-2022 831865       1 2022        1       829.831688
## 832110-2022 832110       3 2022        3       909.372368
## 832248-2022 832248       0 2022        0       179.112719
## 832377-2022 832377      18 2022       18      2791.913155
## 833034-2022 833034       0 2022        0       307.363596
## 847235-2022 847235       1 2022        1       105.138158
## 847567-2022 847567       0 2022        0       146.301754
## 847660-2022 847660       0 2022        0        34.281798
## 848355-2022 848355      24 2022       24      7478.779928
## 853683-2022 853683      27 2022       27     25477.425982
## 853700-2022 853700      48 2022       48     16697.430917
## 853734-2022 853734       0 2022        0      5285.152631
## 853836-2022 853836       1 2022        1       284.737171
## 853887-2022 853887       0 2022        0       266.304496
## 857721-2022 857721      33 2022       33      3389.424667
## 859264-2022 859264      35 2022       35      1721.435964
## 866645-2022 866645       3 2022        3       621.074342
## 866762-2022 866762       0 2022        0      8840.972256
## 870326-2022 870326       0 2022        0        25.300000
## 871140-2022 871140       6 2022        6      1108.420503
## 871565-2022 871565       0 2022        0        58.684539
## 871616-2022 871616      11 2022       11      1590.300875
## 878308-2022 878308       0 2022        0        27.756360
## 878555-2022 878555       0 2022        0        37.769079

#CONCLUSIÓN: las patentes concedidas (patentsg) tienen una relación negativa y estadísticamente significativa con las ventas. El coeficiente de −26.28 indica que, dentro de una misma empresa, una patente concedida adicional se asocia en promedio con una disminución de aproximadamente 26.28 millones de dólares en ventas.

Parte 2. Cuidado de la piel

#Si tuvieras que invertir en alguna sub - categoría, ¿en cuál lo harías? Justifica ampliamente tu respuesta.

df2 <- read.csv("/Users/carlalievanoespinosa/Desktop/business analytics/8vo semestre/df2.csv")

df2 <- read.csv(
  "/Users/carlalievanoespinosa/Desktop/business analytics/8vo semestre/df2.csv",
  skip = 1,
  check.names = FALSE,
  na.strings = c("-", "")
  )

# Subcategorías
categorias <- c(
  "Bath and Shower",
  "Deodorants",
  "Depilatories",
  "Fragrances",
  "Hair Care",
  "Men's Grooming",
  "Skin Care",
  "Sun Care"
)

# Pasar de formato ancho a formato largo
df2_long <- df2 %>%
  select(Category, all_of(as.character(2011:2025))) %>%
  pivot_longer(
    cols = all_of(as.character(2011:2025)),
    names_to = "year",
    values_to = "market_size"
  ) %>%
  mutate(
    year = as.integer(year),
    market_size = parse_number(as.character(market_size))
  )


totales <- df2_long %>%
  filter(Category == "Beauty and Personal Care") %>%
  select(year, total_market = market_size)

# Calcular participación de mercado
df2_model <- df2_long %>%
  filter(Category %in% categorias) %>%
  left_join(totales, by = "year") %>%
  mutate(
    market_share = (market_size / total_market) * 100,
    trend = year - 2011,
    category = factor(Category)
  ) %>%
  arrange(category, year)

# Revisar
head(df2_model)
## # A tibble: 6 × 7
##   Category         year market_size total_market market_share trend category    
##   <chr>           <int>       <dbl>        <dbl>        <dbl> <dbl> <fct>       
## 1 Bath and Shower  2011       8410.      126329.         6.66     0 Bath and Sh…
## 2 Bath and Shower  2012       9085       136020.         6.68     1 Bath and Sh…
## 3 Bath and Shower  2013       9711.      142539.         6.81     2 Bath and Sh…
## 4 Bath and Shower  2014      10227.      149165.         6.86     3 Bath and Sh…
## 5 Bath and Shower  2015      10815.      157656.         6.86     4 Bath and Sh…
## 6 Bath and Shower  2016      11482.      170488.         6.73     5 Bath and Sh…
# Panel
df2_panel <- pdata.frame(
  as.data.frame(df2_model),
  index = c("category", "year")
)

# Revisar estructura del panel
pdim(df2_panel)
## Balanced Panel: n = 8, T = 15, N = 120
# Comprobar si es balanceado
is.pbalanced(df2_panel)
## [1] TRUE
# PRUEBA DE HETEROGENEIDAD

par(mar = c(8, 4, 3, 1))

plotmeans(
  market_share ~ category,
  data = df2_model,
  las = 2,
  xlab = "",
  ylab = "Participación de mercado (%)"
)
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, li, x, pmax(y - gap, li), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped
## Warning in arrows(x, ui, x, pmin(y + gap, ui), col = barcol, lwd = lwd, :
## zero-length arrow is of indeterminate angle and so skipped

# OPCIÓN 1 - MODELO DE REGRESIÓN AGRUPADA (POOLED)

pooled <- plm(
  market_share ~ trend,
  data = df2_panel,
  model = "pooling"
)

summary(pooled)
## Pooling Model
## 
## Call:
## plm(formula = market_share ~ trend, data = df2_panel, model = "pooling")
## 
## Balanced Panel: n = 8, T = 15, N = 120
## 
## Residuals:
##       Min.    1st Qu.     Median    3rd Qu.       Max. 
## -10.616051  -5.734911  -0.098855   6.435351  12.001541 
## 
## Coefficients:
##              Estimate Std. Error t-value Pr(>|t|)    
## (Intercept) 10.488539   1.292050  8.1177 5.16e-13 ***
## trend        0.051766   0.157070  0.3296   0.7423    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    6527
## Residual Sum of Squares: 6521
## R-Squared:      0.00091964
## Adj. R-Squared: -0.0075471
## F-statistic: 0.108618 on 1 and 118 DF, p-value: 0.74231
# OPCIÓN 2 - MODELO DE EFECTOS FIJOS (WITHIN)

within <- plm(
  market_share ~ trend,
  data = df2_panel,
  model = "within",
  effect = "individual"
)

summary(within)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = market_share ~ trend, data = df2_panel, effect = "individual", 
##     model = "within")
## 
## Balanced Panel: n = 8, T = 15, N = 120
## 
## Residuals:
##       Min.    1st Qu.     Median    3rd Qu.       Max. 
## -1.8250909 -0.3761985 -0.0040966  0.2783801  2.6852699 
## 
## Coefficients:
##       Estimate Std. Error t-value Pr(>|t|)   
## trend 0.051766   0.016069  3.2215 0.001674 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    70.204
## Residual Sum of Squares: 64.202
## R-Squared:      0.085501
## Adj. R-Squared: 0.019591
## F-statistic: 10.3779 on 1 and 111 DF, p-value: 0.0016738
# PRUEBA F

prueba_f <- pFtest(within, pooled)

prueba_f
## 
##  F test for individual effects
## 
## data:  market_share ~ trend
## F = 1594.8, df1 = 7, df2 = 111, p-value < 2.2e-16
## alternative hypothesis: significant effects
# OPCIÓN 3 - MODELO DE EFECTOS ALEATORIOS (RANDOM)

random <- plm(
  market_share ~ trend,
  data = df2_panel,
  model = "random",
  effect = "individual"
)

summary(random)
## Oneway (individual) effect Random Effect Model 
##    (Swamy-Arora's transformation)
## 
## Call:
## plm(formula = market_share ~ trend, data = df2_panel, effect = "individual", 
##     model = "random")
## 
## Balanced Panel: n = 8, T = 15, N = 120
## 
## Effects:
##                   var std.dev share
## idiosyncratic  0.5784  0.7605 0.009
## individual    61.4547  7.8393 0.991
## theta: 0.975
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -1.72003 -0.46571 -0.12213  0.22992  2.79033 
## 
## Coefficients:
##              Estimate Std. Error z-value  Pr(>|z|)    
## (Intercept) 10.488539   2.774764  3.7800 0.0001568 ***
## trend        0.051766   0.016069  3.2215 0.0012753 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    74.253
## Residual Sum of Squares: 68.25
## R-Squared:      0.080839
## Adj. R-Squared: 0.073049
## Chisq: 10.3779 on 1 DF, p-value: 0.0012753
# PRUEBA DE HAUSMAN

prueba_hausman <- phtest(within, random)

prueba_hausman
## 
##  Hausman Test
## 
## data:  market_share ~ trend
## chisq = 1.1842e-15, df = 1, p-value = 1
## alternative hypothesis: one model is inconsistent
if(prueba_f$p.value >= 0.05){
  
  modelo_final <- "pooled"
  
} else {
  
  if(prueba_hausman$p.value < 0.05){
    
    modelo_final <- "within"
    
  } else {
    
    modelo_final <- "random"
    
  }
}

modelo_final
## [1] "random"
# CONCLUSIÓN:
# De acuerdo con la prueba de Hausman, el p-value es mayor a 0.05. Por lo tanto, el modelo de Efectos Aleatorios es más adecuado
# que el modelo de Efectos Fijos.

# Se utilizará el modelo Random para realizar la predicción de la participación de mercado de cada subcategoría para 2026.
# Coeficientes del modelo Random
beta0 <- coef(random)["(Intercept)"]
beta1 <- coef(random)["trend"]

beta0
## (Intercept) 
##    10.48854
beta1
##     trend 
## 0.0517657
# Calcular la parte común estimada por el modelo
df2_model <- df2_model %>%
  mutate(
    pred_parte_comun = beta0 + beta1 * trend,
    diferencia = market_share - pred_parte_comun
  )

# Extraer los efectos aleatorios estimados por el modelo
efectos_random <- ranef(random)

efectos_random
## Bath and Shower      Deodorants    Depilatories      Fragrances       Hair Care 
##       -3.988697       -3.273814      -10.150282        4.193032        7.908366 
##  Men's Grooming       Skin Care        Sun Care 
##        4.908848       10.099658       -9.697110
# Crear datos para 2026
nuevo_2026 <- data.frame(
  category = names(efectos_random),
  year = 2026,
  trend = 15
)

# Coeficientes
beta0 <- coef(random)["(Intercept)"]
beta1 <- coef(random)["trend"]

# Incorporar los efectos aleatorios
nuevo_2026$efecto_random <- as.numeric(efectos_random)

# Predicción 2026
nuevo_2026$market_share_2026 <-
  beta0 +
  beta1 * nuevo_2026$trend +
  nuevo_2026$efecto_random

nuevo_2026
##          category year trend efecto_random market_share_2026
## 1 Bath and Shower 2026    15     -3.988697          7.276328
## 2      Deodorants 2026    15     -3.273814          7.991210
## 3    Depilatories 2026    15    -10.150282          1.114743
## 4      Fragrances 2026    15      4.193032         15.458056
## 5       Hair Care 2026    15      7.908366         19.173390
## 6  Men's Grooming 2026    15      4.908848         16.173872
## 7       Skin Care 2026    15     10.099658         21.364682
## 8        Sun Care 2026    15     -9.697110          1.567914
actual_2025 <- df2_model %>%
  filter(year == 2025) %>%
  select(
    category,
    market_share_2025 = market_share
  )

resultado_final <- nuevo_2026 %>%
  select(
    category,
    market_share_2026
  ) %>%
  left_join(actual_2025, by = "category") %>%
  mutate(
    cambio_pp = market_share_2026 - market_share_2025
  ) %>%
  arrange(desc(market_share_2026))

resultado_final
##          category market_share_2026 market_share_2025  cambio_pp
## 1       Skin Care         21.364682        22.9054931 -1.5408111
## 2       Hair Care         19.173390        17.9499600  1.2234302
## 3  Men's Grooming         16.173872        15.7874748  0.3863973
## 4      Fragrances         15.458056        18.0941915 -2.6361351
## 5      Deodorants          7.991210         6.9593731  1.0318371
## 6 Bath and Shower          7.276328         6.8055431  0.4707847
## 7        Sun Care          1.567914         1.3796231  0.1882909
## 8    Depilatories          1.114743         0.5972074  0.5175352

#CONCLUSIÓN: Con base en los resultados del modelo de efectos aleatorios, Hair Care representa la alternativa de inversión más atractiva entre las subcategorías analizadas de Beauty & Personal Care para 2026. #Para 2026, Hair Care presenta una participación de mercado estimada de 19.17%, lo que la posiciona como la segunda subcategoría con mayor participación proyectada, únicamente por debajo de Skin Care, con 21.36%. Esto indica que Hair Care ya cuenta con una presencia importante dentro del mercado total de Beauty & Personal Care, por lo que una inversión en esta categoría no dependería del crecimiento de un segmento pequeño o poco consolidado, sino de una categoría que actualmente representa una proporción significativa del mercado. #Además, Hair Care presenta el mayor incremento absoluto esperado en participación de mercado entre las principales subcategorías, pasando de aproximadamente 17.95% en 2025 a 19.17% en 2026. Esto representa un crecimiento de alrededor de 1.22 puntos porcentuales.

Parte 3. Banco mundial

# obtener esperanza de vida de varios países
life_expectancy_data <- wb_data(country = c("MX", "US", "CA"),
                                indicator = "SP.DYN.LE00.IN",
                                start_date = 1950,end_date = 2025)
# Preparar datos
life_data <- life_expectancy_data %>%
  transmute(
    country = country,
    year = as.integer(date),
    life_expectancy = SP.DYN.LE00.IN
  ) %>%
  filter(!is.na(life_expectancy)) %>%
  mutate(
    trend = year - min(year)
  ) %>%
  arrange(country, year)

head(life_data)
## # A tibble: 6 × 4
##   country  year life_expectancy trend
##   <chr>   <int>           <dbl> <int>
## 1 Canada   1960            71.1     0
## 2 Canada   1961            71.3     1
## 3 Canada   1962            71.4     2
## 4 Canada   1963            71.4     3
## 5 Canada   1964            71.8     4
## 6 Canada   1965            71.9     5
life_panel <- pdata.frame(
  life_data,
  index = c("country", "year")
)

pdim(life_panel)
## Balanced Panel: n = 3, T = 65, N = 195
is.pbalanced(life_panel)
## [1] TRUE
plotmeans(
  life_expectancy ~ country,
  data = life_data,
  xlab = "País",
  ylab = "Esperanza de vida"
)

ggplot(
  life_data,
  aes(
    x = year,
    y = life_expectancy,
    color = country
  )
) +
  geom_line(linewidth = 1) +
  labs(
    title = "Evolución de la esperanza de vida",
    x = "Año",
    y = "Esperanza de vida",
    color = "País"
  ) +
  theme_minimal()

#Opción 1 - Efectos agrupados
pooled_life <- plm(
  life_expectancy ~ trend,
  data = life_panel,
  model = "pooling"
)

summary(pooled_life)
## Pooling Model
## 
## Call:
## plm(formula = life_expectancy ~ trend, data = life_panel, model = "pooling")
## 
## Balanced Panel: n = 3, T = 65, N = 195
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -12.5473  -3.4926   1.8974   3.9455   5.0129 
## 
## Coefficients:
##              Estimate Std. Error t-value  Pr(>|t|)    
## (Intercept) 66.120307   0.656051 100.785 < 2.2e-16 ***
## trend        0.223581   0.017686  12.642 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    7575
## Residual Sum of Squares: 4143.7
## R-Squared:      0.45297
## Adj. R-Squared: 0.45013
## F-statistic: 159.814 on 1 and 193 DF, p-value: < 2.22e-16
#Opción 2 - Efectos fijos
fixed_life <- plm(
  life_expectancy ~ trend,
  data = life_panel,
  model = "within",
  effect = "individual"
)

summary(fixed_life)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = life_expectancy ~ trend, data = life_panel, effect = "individual", 
##     model = "within")
## 
## Balanced Panel: n = 3, T = 65, N = 195
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -6.79967 -0.48439  0.29545  1.11777  3.37249 
## 
## Coefficients:
##        Estimate Std. Error t-value  Pr(>|t|)    
## trend 0.2235814  0.0076021   29.41 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    4188.9
## Residual Sum of Squares: 757.67
## R-Squared:      0.81912
## Adj. R-Squared: 0.81628
## F-statistic: 864.969 on 1 and 191 DF, p-value: < 2.22e-16
prueba_f_life <- pFtest(
  fixed_life,
  pooled_life
)

prueba_f_life
## 
##  F test for individual effects
## 
## data:  life_expectancy ~ trend
## F = 426.79, df1 = 2, df2 = 191, p-value < 2.2e-16
## alternative hypothesis: significant effects
#Opción 3 - Efectos aleatorios
random_life <- plm(
  life_expectancy ~ trend,
  data = life_panel,
  model = "random",
  effect = "individual"
)

summary(random_life)
## Oneway (individual) effect Random Effect Model 
##    (Swamy-Arora's transformation)
## 
## Call:
## plm(formula = life_expectancy ~ trend, data = life_panel, effect = "individual", 
##     model = "random")
## 
## Balanced Panel: n = 3, T = 65, N = 195
## 
## Effects:
##                  var std.dev share
## idiosyncratic  3.967   1.992 0.132
## individual    25.986   5.098 0.868
## theta: 0.9516
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -7.07789 -0.44781  0.37419  1.19318  3.09427 
## 
## Coefficients:
##               Estimate Std. Error z-value  Pr(>|z|)    
## (Intercept) 66.1203066  2.9565836  22.364 < 2.2e-16 ***
## trend        0.2235814  0.0076021  29.410 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    4196.8
## Residual Sum of Squares: 765.61
## R-Squared:      0.81757
## Adj. R-Squared: 0.81663
## Chisq: 864.969 on 1 DF, p-value: < 2.22e-16
prueba_hausman_life <- phtest(
  fixed_life,
  random_life
)

prueba_hausman_life
## 
##  Hausman Test
## 
## data:  life_expectancy ~ trend
## chisq = 8.1205e-15, df = 1, p-value = 1
## alternative hypothesis: one model is inconsistent
# Extraer efectos aleatorios por país
efectos_life <- ranef(random_life)

efectos_life
##        Canada        Mexico United States 
##      3.991447     -5.734168      1.742721
# Coeficientes del modelo Random
beta0 <- coef(random_life)["(Intercept)"]
beta1 <- coef(random_life)["trend"]

beta0
## (Intercept) 
##    66.12031
beta1
##     trend 
## 0.2235814
# Año inicial de la base
anio_inicial <- min(life_data$year)

# Crear base para predicción 2026
nuevo_2026_life <- data.frame(
  country = names(efectos_life),
  year = 2026,
  trend = 2026 - anio_inicial,
  efecto_random = as.numeric(efectos_life)
)

nuevo_2026_life
##         country year trend efecto_random
## 1        Canada 2026    66      3.991447
## 2        Mexico 2026    66     -5.734168
## 3 United States 2026    66      1.742721
# Predicción de esperanza de vida para 2026
nuevo_2026_life <- nuevo_2026_life %>%
  mutate(
    life_expectancy_2026 =
      beta0 +
      beta1 * trend +
      efecto_random
  )

nuevo_2026_life
##         country year trend efecto_random life_expectancy_2026
## 1        Canada 2026    66      3.991447             84.86813
## 2        Mexico 2026    66     -5.734168             75.14251
## 3 United States 2026    66      1.742721             82.61940
resultado_2026_life <- nuevo_2026_life %>%
  select(
    country,
    life_expectancy_2026
  ) %>%
  arrange(desc(life_expectancy_2026))

resultado_2026_life
##         country life_expectancy_2026
## 1        Canada             84.86813
## 2 United States             82.61940
## 3        Mexico             75.14251
max(life_data$year)
## [1] 2024
ultimo_anio <- max(life_data$year)

actual_life <- life_data %>%
  filter(year == ultimo_anio) %>%
  select(
    country,
    life_expectancy_actual = life_expectancy
  )

resultado_final_life <- resultado_2026_life %>%
  left_join(
    actual_life,
    by = "country"
  ) %>%
  mutate(
    cambio = life_expectancy_2026 -
      life_expectancy_actual
  ) %>%
  arrange(desc(life_expectancy_2026))

resultado_final_life
##         country life_expectancy_2026 life_expectancy_actual     cambio
## 1        Canada             84.86813               82.10805  2.7600790
## 2 United States             82.61940               78.89024  3.7291577
## 3        Mexico             75.14251               75.26400 -0.1214876

#COCLUSIÓN: De acuerdo con el modelo de efectos aleatorios, Canadá presenta la mayor esperanza de vida estimada para 2026, con aproximadamente 84.87 años, seguido por Estados Unidos con 82.62 años y México con 75.14 años. Los resultados muestran que las diferencias estructurales entre los países se mantendrían, con una brecha proyectada de aproximadamente 9.73 años entre Canadá y México y de 7.48 años entre Estados Unidos y México.

file <- "Actividad_1_patentes.Rmd"

x <- readLines(
  file,
  encoding = "UTF-8",
  warn = FALSE
)
LS0tCnRpdGxlOiAiQWN0aXZpZG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