#cargando las librerias
library(readxl)
library(ggplot2)
library(plotly)
## 
## 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(lmtest)
## Warning: package 'lmtest' was built under R version 4.5.3
## Cargando paquete requerido: zoo
## Warning: package 'zoo' was built under R version 4.5.3
## 
## Adjuntando el paquete: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(CGPfunctions)
#cargar datos
data1= read_excel("D:/GOMBRA/TRABAJO/DOCENCIA/U Catolica/Curso Herramientas MIneria de Datos 2026_II/Datos1.xlsx")
mod1=lm(Precio_accion~Precio_WTIxBarril, data = data1)
summary(mod1)
## 
## Call:
## lm(formula = Precio_accion ~ Precio_WTIxBarril, data = data1)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -59.90 -40.74 -15.94  33.40 136.82 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)   
## (Intercept)        177.768    232.828   0.764  0.45627   
## Precio_WTIxBarril   26.192      6.542   4.004  0.00102 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 57.13 on 16 degrees of freedom
## Multiple R-squared:  0.5005, Adjusted R-squared:  0.4692 
## F-statistic: 16.03 on 1 and 16 DF,  p-value: 0.001024
#exploracion bivariada
g1=ggplot(data=data1,mapping=
            aes(x=Precio_WTIxBarril,y=Precio_accion))+geom_point()+theme_bw()+
            geom_smooth()       
ggplotly(g1)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
g2 = ggplot(data = data1, mapping = aes(x = Precio_WTIxBarril, y = Precio_accion)) +   geom_point() +   theme_bw() +   geom_smooth(method = "lm", color = "red", se = FALSE) 
ggplotly(g2)
## `geom_smooth()` using formula = 'y ~ x'
par(mfrow=c(2,2))
plot(mod1)

#correlacion entre -1 y 1
cor(data1$Precio_WTIxBarril,data1$Precio_accion)
## [1] 0.7074373

Analisis Grafico

#Boxplot
b1= ggplot (data=data1, aes(x="",y=Precio_accion))+
  geom_boxplot(fill = "tomato",color="black",outlier.color = "red")+
  theme_bw()+
  labs(
  title ="Boxplot de Precio de la Acción",
  x= "",
  y= "Precio de Acción"
)

ggplotly(b1)
# Crear el histograma
h1 = ggplot(data = data1, aes(x = Precio_accion)) +
  geom_histogram(bins = 30, fill = "steelblue", color = "white") +
  theme_bw() +
  labs(
    title = "Histograma del Precio de Acción",
    x = "Precio de Acción",
    y = "Frecuencia"
  )

# Hacerlo interactivo
ggplotly(h1)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.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
data1 %>%
  summarise(
    Media = mean(Precio_accion, na.rm = TRUE),
    Mediana = median(Precio_accion, na.rm = TRUE),
    Desv_Estandar = sd(Precio_accion, na.rm = TRUE),
    Minimo = min(Precio_accion, na.rm = TRUE),
    Maximo = max(Precio_accion, na.rm = TRUE),
    Total_Datos = n()
  )
## # A tibble: 1 × 6
##   Media Mediana Desv_Estandar Minimo Maximo Total_Datos
##   <dbl>   <dbl>         <dbl>  <dbl>  <dbl>       <int>
## 1 1108.    1120          78.4    955   1230          18