library(lsm)      # Para descargar una base de datos
library(dplyr)
## 
## 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(moments)  # Para hallar las medidas de forma
library(e1071)
## 
## Adjuntando el paquete: 'e1071'
## The following objects are masked from 'package:moments':
## 
##     kurtosis, moment, skewness
library(ggplot2)
## 
## Adjuntando el paquete: 'ggplot2'
## The following object is masked from 'package:e1071':
## 
##     element
datosCompleto <- lsm::survey
Muestra <- datosCompleto[1:100,]

Sexo <- as.factor(Muestra$Gender)  
Fuma <- as.factor(Muestra$Smoke) 

Tablas

ggplot(Muestra, aes(x = Sexo)) +                            #1
  #geom_bar() +                                             #2
  geom_bar(width=0.5, colour="blue", fill="purple") +       #2 
  
  labs(x="Sexo",y= "Frecuencia")  +              #3               
  ylim(c(0,60)) +                               #4
  #xlim(c(0,300)) +                              #4
  ggtitle("Diagrama de barras")  +               #5
  
  # theme_bw() +                                 #6
  theme_bw(base_size = 12) +                     #6
  #coord_flip() +                                #7
  
  geom_text(aes(label=..count..), stat='count',  #8
            position=position_dodge(0.9), 
            vjust=-0.5, 
            size=5.0
            ) + 
  facet_wrap(~"Variable Sexo")                   #9

En la gráfica podemos observar que la cantidad de hombres y mujeres es muy similar, porque se trata de 51 hombres (51%) y 49 mujeres (49%). Los hombres por lo tanto, constituyen una proporción ligeramente mayor de la muestra con una diferencia de 2 personas.

Tabla doble.

ggplot(Muestra, aes(Fuma,  fill=Sexo)) +      
  geom_bar(position="dodge",colour="green") +
  
  labs(x= "Fuma",  y="Frecuencias", fill="Sexo") +
  ylim(c(0,30)) +
  #xlim(c(0,300)) +   
  
  ggtitle("Diagrama de barras") + 
  
  #theme_bw() +                     
  theme_bw(base_size = 12) +          
  #coord_flip() + 
  
  #guides(fill=FALSE)+                                #8                                    
  scale_fill_manual(values = c("purple","pink")) +   #9
  
  geom_text(aes(label=..count..), stat='count',       #10
            position=position_dodge(0.9),
            vjust=-0.5, 
            size=5.0
            )+
  
  facet_wrap(~"Sexo por fumadores y no fumadores")  #11

El gráfico nos indica que de las 100 personas estudiadas, 55 son fumadoras y 45 no lo son, de los fumadores, 28 son mujeres y 27 son hombres, de los no fumadores, 21 son mujeres y 24 son hombres. Se observa que el número de mujeres fumadoras es ligeramente superior al de hombres fumadores.

Tabla doble intercambio

ggplot(Muestra, aes(Sexo,  fill=Fuma)) +      
  geom_bar(position="dodge",colour="black") +
  
  labs(x= "Sexo",  y="Frecuencias", fill="Fuma") +
  ylim(c(0,30)) +
  #xlim(c(0,300)) +   
  
  ggtitle("Diagrama de barras") + 
  
  #theme_bw() +                     
  theme_bw(base_size = 12) +          
  #coord_flip() + 
  
  #guides(fill=FALSE)+                                #8                                    
  scale_fill_manual(values = c("blue","yellow")) +   #9
  
  geom_text(aes(label=..count..), stat='count',       #10
            position=position_dodge(0.9),
            vjust=-0.5, 
            size=5.0
            )+
  
  facet_wrap(~"Fuma por genero")  #11

Las gráficas nos muestran que 55 personas son fumadoras y 45 no lo son de ellos, 28 son mujeres fumadoras y 27 son hombres fumadores; mientras que 21 son mujeres no fumadoras y 24 son hombres no fumadores, los datos no cambiaron, sólo la forma de presentar la información, se modificaron las variables del eje y los colores.

Medidas estadisticas

str(Muestra)
## tibble [100 × 66] (S3: tbl_df/tbl/data.frame)
##  $ Observation : num [1:100] 1 2 3 4 5 6 7 8 9 10 ...
##  $ ID          : chr [1:100] "SB11201910010435" "SB11201910004475" "SB11201910011427" "SB11201910041975" ...
##  $ Gender      : chr [1:100] "Female" "Male" "Male" "Male" ...
##  $ Like        : chr [1:100] "TV" "Network" "Network" "TV" ...
##  $ Age         : num [1:100] 21.4 21.1 20.9 18.4 16.6 ...
##  $ Smoke       : chr [1:100] "No" "Yes" "Yes" "Yes" ...
##  $ Height      : num [1:100] 1.58 1.6 1.5 1.53 1.78 1.65 1.73 1.53 1.64 1.52 ...
##  $ Weight      : num [1:100] 75 80 64 49 82 80 90 55 50 78 ...
##  $ BMI         : num [1:100] 30 31.2 28.4 20.9 25.9 ...
##  $ School      : chr [1:100] "Private" "Public" "Private" "Public" ...
##  $ SES         : chr [1:100] "Medium" "High" "High" "Low" ...
##  $ Enrollment  : chr [1:100] "Credit" "Scholarship" "Scholarship" "Credit" ...
##  $ Score       : num [1:100] 81 78 77 70 68 65 54 50 36 35 ...
##  $ MotherHeight: chr [1:100] "Short_M" "Normal_M" "Normal_M" "Tall_M" ...
##  $ MotherAge   : num [1:100] 41 45 45 45 46 46 47 48 48 48 ...
##  $ MotherCHD   : num [1:100] 0 0 0 0 1 0 0 0 0 1 ...
##  $ FatherHeight: chr [1:100] "Normal_F" "Short_F" "Tall_F" "Short_F" ...
##  $ FatherAge   : num [1:100] 40 43 44 45 45 46 46 48 48 49 ...
##  $ FatherCHD   : num [1:100] 1 1 1 2 1 1 1 1 1 1 ...
##  $ Status      : chr [1:100] "Distinguished" "Distinguished" "Distinguished" "Regular" ...
##  $ SemAcum     : num [1:100] 4.25 2.8 4.15 3.2 3.45 2.75 2.7 4.35 4.3 2.8 ...
##  $ Exam1       : num [1:100] 1.5 2.3 3.4 2.5 3.1 3.8 5 4 2.5 2.4 ...
##  $ Exam2       : num [1:100] 5 4.9 3.6 4.2 3.5 4.4 3 2.3 3.3 2.6 ...
##  $ Exam3       : num [1:100] 5 3.7 2 5 5 4.2 3.5 4.6 3.8 4.3 ...
##  $ Exam4       : num [1:100] 4.5 3.3 1.9 2.5 3 5 3.6 4.3 1.9 5 ...
##  $ ExamAcum    : num [1:100] 16 14.2 10.9 14.2 14.6 17.4 15.1 15.2 11.5 14.3 ...
##  $ Definitive  : num [1:100] 4 3.55 2.73 3.55 3.65 ...
##  $ Expense     : num [1:100] 48.9 72.1 85.2 56.6 64.6 63 40.8 65.4 37.3 63 ...
##  $ Income      : num [1:100] 1.61 2.07 2.84 1.55 2.32 2.1 1.69 2.18 1.71 2.1 ...
##  $ Gas         : num [1:100] 27.4 24.2 22.3 23.1 27.3 ...
##  $ Course      : chr [1:100] "Face-to-Face" "Virtual" "Face-to-Face" "Virtual" ...
##  $ Law         : chr [1:100] "Agree" "Agree" "Agree" "Agree" ...
##  $ Economic    : chr [1:100] "Regular" "Good" "Regular" "Bad" ...
##  $ Race        : chr [1:100] "Ethnic" "Ethnic" "Ethnic" "Ethnic" ...
##  $ Region      : chr [1:100] "North" "Center" "North" "Center" ...
##  $ EMO1        : num [1:100] 1 4 3 4 2 3 2 3 4 2 ...
##  $ EMO2        : num [1:100] 2 4 1 2 1 1 4 1 2 2 ...
##  $ EMO3        : num [1:100] 2 1 3 3 2 4 2 4 3 3 ...
##  $ EMO4        : num [1:100] 1 2 3 1 4 2 3 2 1 1 ...
##  $ EMO5        : num [1:100] 4 1 2 2 2 2 1 1 2 2 ...
##  $ GOAL1       : chr [1:100] "Strongly agree" "Undecided" "Agree" "Agree" ...
##  $ GOAL2       : chr [1:100] "Agree" "Disagree" "Disagree" "Undecided" ...
##  $ GOAL3       : chr [1:100] "Strongly agree" "Disagree" "Agree" "Strongly agree" ...
##  $ Pre_STAT1   : num [1:100] 2 1 5 4 1 4 4 2 2 2 ...
##  $ Pre_STAT2   : num [1:100] 4 1 1 3 4 1 2 3 3 5 ...
##  $ Pre_STAT3   : num [1:100] 2 1 3 1 1 5 4 3 3 2 ...
##  $ Pre_STAT4   : num [1:100] 5 1 1 2 2 3 2 3 2 4 ...
##  $ Post_STAT1  : num [1:100] 4 5 5 3 5 2 3 3 2 5 ...
##  $ Post_STAT2  : num [1:100] 5 1 2 2 3 3 2 3 2 3 ...
##  $ Post_STAT3  : num [1:100] 2 3 3 4 3 5 5 4 5 4 ...
##  $ Post_STAT4  : num [1:100] 2 3 3 5 4 4 3 5 5 1 ...
##  $ Pre_IDARE1  : chr [1:100] "Quite a bit" "Quite a bit" "Quite a bit" "Little" ...
##  $ Pre_IDARE2  : chr [1:100] "Little" "Little" "Little" "Nothing" ...
##  $ Pre_IDARE3  : chr [1:100] "Quite a bit" "A lot" "Quite a bit" "Quite a bit" ...
##  $ Pre_IDARE4  : chr [1:100] "Quite a bit" "Nothing" "Quite a bit" "Quite a bit" ...
##  $ Pre_IDARE5  : chr [1:100] "Little" "Quite a bit" "Little" "Nothing" ...
##  $ Post_IDARE1 : chr [1:100] "A lot" "A little" "Nothing" "Quite a bit" ...
##  $ Post_IDARE2 : chr [1:100] "A lot" "Nothing" "Quite a bit" "A little" ...
##  $ Post_IDARE3 : chr [1:100] "A little" "Quite a bit" "Nothing" "A lot" ...
##  $ Post_IDARE4 : chr [1:100] "Quite a bit" "A lot" "Nothing" "Quite a bit" ...
##  $ Post_IDARE5 : chr [1:100] "A lot" "Quite a bit" "Nothing" "A lot" ...
##  $ PSICO1      : chr [1:100] "Frequently" "Frequently" "Sometimes" "Almost always" ...
##  $ PSICO2      : chr [1:100] "Almost always" "Sometimes" "Sometimes" "Frequently" ...
##  $ PSICO3      : chr [1:100] "Frequently" "Sometimes" "Sometimes" "Frequently" ...
##  $ PSICO4      : chr [1:100] "Almost always" "Frequently" "Frequently" "Almost never" ...
##  $ PSICO5      : chr [1:100] "Almost always" "Frequently" "Sometimes" "Sometimes" ...
Muestra2 <- datosCompleto[1:100,] 
x <- as.numeric(Muestra2$Exam3)  # A) Convirtiendo la variable a numérica
x
##   [1] 5.0 3.7 2.0 5.0 5.0 4.2 3.5 4.6 3.8 4.3 3.0 3.8 3.4 3.3 3.5 4.5 3.6 4.0
##  [19] 3.4 4.0 4.2 3.5 3.7 4.0 4.0 3.2 2.9 2.9 3.0 3.3 2.8 2.4 3.8 3.3 3.2 2.2
##  [37] 2.6 3.2 3.3 1.2 4.2 2.4 5.0 2.8 3.0 3.8 3.2 1.5 2.6 3.8 3.2 3.3 1.4 3.8
##  [55] 1.4 3.6 3.6 2.4 2.8 3.1 2.4 1.8 1.6 3.3 4.4 1.0 4.5 2.0 4.2 4.2 3.1 2.3
##  [73] 2.6 2.7 2.4 2.2 2.8 2.4 1.9 2.4 1.7 2.9 2.4 2.2 2.8 3.2 3.1 2.7 2.5 3.5
##  [91] 3.3 2.1 3.3 2.1 3.7 5.0 3.7 2.0 5.0 5.0
min(x)          #B) Mínimo
## [1] 1
max(x)          #C) Máximo
## [1] 5
range(x)        #D) Obtenemos (min, max)
## [1] 1 5
length(x)       #E) Tamaño
## [1] 100
sum(x)          #F) Suma los valores de los datos
## [1] 317.6
mean(x)         #G) Media aritmética
## [1] 3.176
median(x)       #H) Mediana
## [1] 3.2
var(x)          #I) Varianza muestral
## [1] 0.8885091
sqrt(var(x))    #J) Desviación estándar muestral (una forma)
## [1] 0.9426076
sd(x)           #K) Desviación estándar muestral (otra forma)
## [1] 0.9426076
skewness(x)     #L) Sesgo
## [1] 0.01846742
quantile(x, probs=0.80)    #M) 80-ésimo percentil o percentil 85
## 80% 
##   4
quantile(x, probs=0.25)    #N) Primer cuartil o 25-ésimo percentil
## 25% 
## 2.4
quantile(x, probs=0.50)    #O) Segundo cuartil o 50-ésimo percentil o mediana
## 50% 
## 3.2
quantile(x, probs=0.75)    #P) Tercer cuartil o 75-ésimo percentil
## 75% 
## 3.8
quantile(x, probs=0.85)    #M) 80-ésimo percentil o percentil 85
## 85% 
## 4.2