library(readxl)
## Warning: package 'readxl' was built under R version 4.4.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## 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
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(plotly)
## Warning: package 'plotly' was built under R version 4.4.3
##
## Adjuntando el paquete: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(knitr)
## Warning: package 'knitr' was built under R version 4.4.3
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.4.3
##
## Adjuntando el paquete: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.4.3
library(RecordLinkage)
## Warning: package 'RecordLinkage' was built under R version 4.4.3
## Cargando paquete requerido: DBI
## Warning: package 'DBI' was built under R version 4.4.3
## Cargando paquete requerido: RSQLite
## Warning: package 'RSQLite' was built under R version 4.4.3
## Cargando paquete requerido: ff
## Warning: package 'ff' was built under R version 4.4.3
## Cargando paquete requerido: bit
## Warning: package 'bit' was built under R version 4.4.3
##
## Adjuntando el paquete: 'bit'
## The following object is masked from 'package:dplyr':
##
## symdiff
## The following object is masked from 'package:base':
##
## xor
## Attaching package ff
## - getOption("fftempdir")=="C:/Users/Usuario/AppData/Local/Temp/RtmpW29UIq/ff"
## - getOption("ffextension")=="ff"
## - getOption("ffdrop")==TRUE
## - getOption("fffinonexit")==TRUE
## - getOption("ffpagesize")==65536
## - getOption("ffcaching")=="mmnoflush" -- consider "ffeachflush" if your system stalls on large writes
## - getOption("ffbatchbytes")==16777216 -- consider a different value for tuning your system
## - getOption("ffmaxbytes")==536870912 -- consider a different value for tuning your system
##
## Adjuntando el paquete: 'ff'
## The following objects are masked from 'package:utils':
##
## write.csv, write.csv2
## The following objects are masked from 'package:base':
##
## is.factor, is.ordered
## RecordLinkage library
## [c] IMBEI Mainz
##
## Adjuntando el paquete: 'RecordLinkage'
## The following object is masked from 'package:bit':
##
## clone
## The following object is masked from 'package:base':
##
## isFALSE
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.4.3
library(htmltools)
## Warning: package 'htmltools' was built under R version 4.4.3
library(readxl)
library(dplyr)
library(ggplot2)
library(plotly)
library(knitr)
library(kableExtra)
library(tidyr)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.3
## Warning: package 'readr' was built under R version 4.4.3
## Warning: package 'purrr' was built under R version 4.4.3
## Warning: package 'stringr' was built under R version 4.4.3
## Warning: package 'forcats' was built under R version 4.4.3
## Warning: package 'lubridate' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ lubridate 1.9.4 ✔ stringr 1.5.1
## ✔ purrr 1.0.4 ✔ tibble 3.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ plotly::filter() masks dplyr::filter(), stats::filter()
## ✖ kableExtra::group_rows() masks dplyr::group_rows()
## ✖ dplyr::lag() masks stats::lag()
## ✖ bit::symdiff() masks dplyr::symdiff()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
require(ggplot2)
require(ggpubr)
## Cargando paquete requerido: ggpubr
## Warning: package 'ggpubr' was built under R version 4.4.3
require(CGPfunctions)
## Cargando paquete requerido: CGPfunctions
## Warning: package 'CGPfunctions' was built under R version 4.4.3
require(plotly)
library(rsconnect)
## Warning: package 'rsconnect' was built under R version 4.4.3
library(packrat)
## Warning: package 'packrat' was built under R version 4.4.3
library(readxl) # Para leer archivos Excel
library(tseries) # Para pruebas de estacionariedad
## Warning: package 'tseries' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(forecast) # Para modelado ARIMA y pronósticos
## Warning: package 'forecast' was built under R version 4.4.3
##
## Adjuntando el paquete: 'forecast'
##
## The following object is masked from 'package:ggpubr':
##
## gghistogram
library(ggplot2) # Para visualización de datos
library(plotly) # Para gráficos interactivos
library(timetk)
## Warning: package 'timetk' was built under R version 4.4.3
library(readxl)
data_col <- read_excel("E:/JAVERIANA/MAESTRIA EN FINANZAS/INTELIGENCIA DE NEGOCIOS/SEMANA2/PACCIONES COL.xlsx",
col_types = c("date", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric"))
## New names:
## • `` -> `...1`
View(data_col)
#trae los datos de los cierres mensuales del valor de las acciones
# Convertir/declarar el número de microempresas en serie de tiempo trimestral
micro_ts <- ts(data_col$ISA, start = c(2010, 1), frequency = 12)
micro_ts
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 2010 12680 12880 12800 12980 12720 12800 13080 12960 14100 14600 13500 14100
## 2011 13200 12780 12720 12540 13040 12860 12240 12200 11600 11900 10520 11200
## 2012 11220 11400 11300 11400 11420 11600 11400 10960 9520 9920 9500 9600
## 2013 9990 9800 9000 8400 7940 7570 8040 9240 9450 9360 8620 9100
## 2014 7420 7600 8790 9080 9000 9170 9230 9700 9260 8800 8200 8630
## 2015 7500 7810 7430 8000 8190 7350 7080 6640 7150 6940 6800 7380
## 2016 7360 7750 8650 8700 8800 8940 9090 9900 9750 9990 9900 9980
## 2017 10560 11200 11600 11660 14100 13340 13680 13360 13680 13320 13800 14200
## 2018 14300 13700 13320 14480 14160 14500 14200 13520 13380 11960 12940 13980
## 2019 13920 13980 15960 15820 16200 17860 17800 18180 18240 19500 18980 19600
## 2020 18800 18600 15480 18200 19600 18800 19200 20940 20380 20860 22700 25700
## 2021 23060 23800 22500 21380 20020 22150 21820 22700 22700 22610 22300 22400
## 2022 23600 24300 24300 21760 23780 20500 20600 19170 16500 19270 19500 21000
## 2023 19040 17210 15150 18200 17600 16890 17170 14980 14920 14500 15160 15480
## 2024 16860 16720 19780 18100 19140 18200 17500 18160 17080 17580 16800 16700
## 2025 18480 20300
# Calcular estadísticas descriptivas básicas
descriptive_stats <- data.frame(
Min = min(micro_ts),
Max = max(micro_ts),
Media = mean(micro_ts),
Mediana = median(micro_ts),
DesviacionEstandar = sd(micro_ts),
CoefVar = sd(micro_ts) / mean(micro_ts)
)
print(descriptive_stats)
## Min Max Media Mediana DesviacionEstandar CoefVar
## 1 6640 25700 14161.37 13440 4828.716 0.340978
library(ggplot2)
library(plotly)
# Gráfico interactivo de la serie original
grafico_serie <- ggplot(data_col, aes(x = seq.Date(from = as.Date("2010-01-01"), by = "month", length.out = nrow(data_col)), y = micro_ts)) +
geom_line(color = "grey", size = 0.4) +
geom_point(color = "black", size = 0.1) +
ggtitle("Figura 1. evolucion de la accion de ISA") +
xlab("Tiempo") +
ylab("valor de la accion") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
ggplotly(grafico_serie)
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
# Cargar librerías necesarias
library(ggplot2)
library(plotly)
# Descomposición de la serie temporal
stl_decomp <- stl(micro_ts, s.window = "periodic")
# Convertir la descomposición a un data frame para graficar con ggplot2
stl_df <- data.frame(
Time = rep(time(micro_ts), 12), # Tiempo repetido para cada componente
Value = c(stl_decomp$time.series[, "seasonal"],
stl_decomp$time.series[, "trend"],
stl_decomp$time.series[, "remainder"],
micro_ts),
Component = rep(c("Estacional", "Tendencia", "Residuo", "Serie Original"), each = length(micro_ts))
)
# Crear gráfico con ggplot2
p <- ggplot(stl_df, aes(x = Time, y = Value, color = Component)) +
geom_line() +
facet_wrap(~Component, scales = "free_y", ncol = 1) +
theme_minimal() +
labs(title = "Figura A. Descomposición de la serie de tiempo de la accion de ISA",
x = "Tiempo",
y = "Valor")
# Convertir a gráfico interactivo con plotly
ggplotly(p)
# Extraer los componentes de la descomposición
micro_sa <- micro_ts - stl_decomp$time.series[, "seasonal"]
# Crear vector de fechas correctamente alineado con la serie
fechas <- seq.Date(from = as.Date("2010-01-01"), by = "month", length.out = length(micro_ts))
# Gráfico mejorado con fechas en el eje X
grafico_ajustada <- ggplot() +
geom_line(aes(x = fechas, y = micro_ts), color = "grey", size = 0.5, linetype = "solid", name = "Serie Original") +
geom_line(aes(x = fechas, y = micro_sa), color = "black", size = 0.6, linetype = "solid", name = "Serie Ajustada") +
ggtitle("Figura 2. valor de la accion de ISA :Serie Original vs Serie Ajustada por Estacionalidad") +
xlab("Tiempo") +
ylab("valor de la accion de ISA") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Rotar etiquetas para mejor visualización
## Warning in geom_line(aes(x = fechas, y = micro_ts), color = "grey", size = 0.5,
## : Ignoring unknown parameters: `name`
## Warning in geom_line(aes(x = fechas, y = micro_sa), color = "black", size =
## 0.6, : Ignoring unknown parameters: `name`
# Convertir a gráfico interactivo
ggplotly(grafico_ajustada)
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
#cotrastamos la serie de tiempo original con la linea de tendencia
# Extraer la tendencia de la descomposición STL
tendencia <- stl_decomp$time.series[, "trend"]
# Gráfico interactivo de la serie original vs tendencia
grafico_tendencia <- ggplot() +
geom_line(aes(x = fechas, y = micro_ts), color = "grey", size = 0.7, linetype = "solid", name = "Serie Original") +
geom_line(aes(x = fechas, y = tendencia), color = "black", size = 0.8, linetype = "solid", name = "Tendencia") +
ggtitle("Figura 3. Microempresas:Serie Original vs Tendencia") +
xlab("Tiempo") +
ylab("Número de microempresas") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Rotar etiquetas del eje X para mejor visualización
## Warning in geom_line(aes(x = fechas, y = micro_ts), color = "grey", size = 0.7,
## : Ignoring unknown parameters: `name`
## Warning in geom_line(aes(x = fechas, y = tendencia), color = "black", size =
## 0.8, : Ignoring unknown parameters: `name`
# Convertir a gráfico interactivo
ggplotly(grafico_tendencia)
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
#Cálculo de la tasa de crecimiento anual correctamente alineada
tasa_crecimiento <- (micro_ts[(13:length(micro_ts))] / micro_ts[1:(length(micro_ts) - 12)] - 1) * 100
tasa_tendencia <- (tendencia[(13:length(tendencia))] / tendencia[1:(length(tendencia) - 12)] - 1) * 100
# Crear vector de fechas corregido
fechas_corregidas <- seq(from = as.Date("2011-01-01"), by = "month", length.out = length(tasa_crecimiento))
# Verificar longitudes
print(length(fechas_corregidas))
## [1] 170
print(length(tasa_crecimiento))
## [1] 170
print(length(tasa_tendencia))
## [1] 170
# Gráfico de la tasa de crecimiento anual
grafico_crecimiento <- ggplot() +
geom_line(aes(x = fechas_corregidas, y = tasa_crecimiento), color = "grey", size = 0.7) +
geom_line(aes(x = fechas_corregidas, y = tasa_tendencia), color = "black", size = 0.8, linetype = "dashed") +
ggtitle("Figura 4. valor accion ISA: Tasa de crecimiento anual % de la serie Original vs Tendencia") +
xlab("Tiempo") +
ylab("% de Crecimiento Anual") +
theme_minimal()
# Convertir a gráfico interactivo
ggplotly(grafico_crecimiento)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.