##install.packages("MASS")
##install.packages("UsingR")
##install.packages("examplePackage")
##install.packages("ggplot2")
library(dslabs)
library(viridisLite)
library(RColorBrewer)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(datasets)

##install.packages("ggplot2")
# Instalar y cargar ggplot2 si aún no lo has hecho

library(ggplot2)
##install.packages("UsingR")
##install.packages("plotly")
library(plotly)
## 
## Attaching package: '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(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:plotly':
## 
##     select
## The following object is masked from 'package:dplyr':
## 
##     select
library(UsingR)
## Loading required package: HistData
## Loading required package: Hmisc
## 
## Attaching package: 'Hmisc'
## The following object is masked from 'package:plotly':
## 
##     subplot
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following objects are masked from 'package:base':
## 
##     format.pval, units
datos <- data.frame(brillo = brightness)

# A
ggplot(datos, aes(x = brillo)) +
  geom_histogram(aes(y = ..density..), bins = 30, fill = "lightblue", color = "black") +
  geom_density(alpha = 0.2, fill = "red") +
  labs(title = "Histograma y Curva de Densidad del Brillo de Estrellas",
       x = "Brillo",
       y = "Densidad") +
  theme_minimal()
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#B

boxplot(brightness,
        main = "Boxplot del Brillo de Estrellas",
        ylab = "Brillo",
        col = "lightblue")

stats <- boxplot.stats(brightness)$out
stats_sorted <- sort(stats)
print(stats_sorted)
##  [1]  2.07  2.28  3.88  4.37  4.55  4.61  4.78  4.89  4.99  5.01  5.04  5.13
## [13]  5.24  5.29  5.41  5.42  5.53  5.54  5.55 11.28 11.55 11.55 11.55 11.63
## [25] 11.65 11.67 11.71 11.73 11.79 11.80 11.99 12.04 12.04 12.14 12.17 12.19
## [37] 12.31 12.43
## el segundo mejor outlier es 2.28

##C

limiteInf <- 5.6
limiteSup <- 11

brightness_without <- brightness[brightness >= limiteInf & brightness <= limiteSup]

length(brightness_without) 
## [1] 918
## D

# la moda es 8,55, tiene una distribubion en campana con un solo pico y de igual forma
# tiene varios valores atipicos que pueden observarse en bocplot


##3