Felicidad marital y tener hijos, análisis en R.
Visualización de la felicidad marital según la presencia de hijos usando tablas de contingencia en R
Este proyecto en R explora la relación entre la felicidad en el matrimonio y tener hijos mediante tablas de contingencia, gráficos circulares y de barras. Se usa la base de datos affairs del paquete wooldridge, visualizando las frecuencias absolutas y relativas para ofrecer una interpretación clara y didáctica del cruce de variables cualitativas.
1install.packages("wooldridge")
2install.packages("RColorBrewer")
3
4# Call the library and data
5library(RColorBrewer)
6library(wooldridge)
7data(affairs, package = "wooldridge")
8
9# Qualification of persons having children
10haskid <- c("no", "yes")
11kids <- factor(affairs$kids, labels = haskid)
12
13# Call data rating happiness of Marriage
14mlab <- c("very unhappy", "unhappy", "averge", "happy", "very happy")
15marriage <- factor(affairs$ratemarr, labels = mlab)
16
17countslab <- table(marriage, kids)
18percentage <- prop.table(countslab, margin = 1)
19percentage2 <- prop.table(countslab, margin = 2)
20
21# Pie Graph
22percent <- round(c(prop.table(table(marriage))*100), digits = 2)
23
24color <- brewer.pal(length(mlab), "Accent")
25par(mai = c(0,0,0,0))
26pie(table(marriage),
27 col = color,
28 border = "white",
29 labels = paste(percent, "%"),
30 radius = 0.6)
31title("Rating of marriage happiness",
32 line = -4)
33legend(
34 -0.95, -0.85,
35 legend = c("very unhappy", "unhappy", "averge", "happy", "very happy"),
36 horiz = TRUE,
37 col = color,
38 fill = color,
39 cex = 0.8)
1# Graph distribution of happiness
2par(mai = c(0.7, 1.4, 0.9, 0.5))
3barplot(table(marriage),
4 horiz = TRUE, las = 1,
5 col = color)
6title("Distribution of Happines")
7legend(
8 170, 2.3,
9 legend = table(marriage),
10 col = color,
11 fill = color,
12 cex = 1.2)
1# Distribution of marriages with children
2percent2 <- prop.table(table(kids)) * 100
3p <- round(percent2, digits = 2)
4color2 <- brewer.pal(length(haskid), "Set1")
5par(mai = c(0, 0, 0, 0))
6pie(table(kids),
7 col = color2,
8 border = "white",
9 labels = paste(p, "%"),
10 radius = 0.6)
11title("Distribution of marriages with children",
12 line = -4)
13legend(
14 0.6, -0.7,
15 legend = c("no", "yes"),
16 col = color,
17 fill = color2,
18 cex = 1.3)
1barplot(table(kids),
2 col = color2,
3 main = "Distribution of marriages with children")
4legend("topleft",
5 legend = table(kids),
6 col = color2,
7 fill = color2)
1color3 <- brewer.pal(length(mlab), "Paired")
2par(mai = c(1, 1.5, 1, 1))
3barplot(table(kids, marriage),
4 col = color3,
5 xlim = c(0, 250),
6 xlab = "Quantity",
7 horiz = TRUE, las = 1)
8title("Happiness by kids")
9legend(
10 170, 2.3,
11 legend = c("no", "yes"),
12 col = color3,
13 fill = color3,
14 cex = 1.3)
1barplot(prop.table(table(kids, marriage), margin = 2),
2 col = color3,
3 main = "Happiness by kids",
4 ylab = "Percentage")
5legend(
6 3.6, 0.94,
7 horiz = TRUE,
8 legend = c("no", "yes"),
9 col = color3,
10 fill = color3,
11 cex = 1.3)
Explora el Proyecto
Contenido Relacionado
Tutorial Analisis de datos con R
En este capítulo exploramos el ciclo de trabajo del análisis de datos en R, desde la carga y transformación de datos hasta la creación de gráficos y funciones propias
El Proyecto R para Cálculo Estadístico
R es un entorno de software libre para computación estadística y gráficos. Se compila y ejecuta en una amplia variedad de plataformas UNIX, Windows y macOS.
Comentarios
Cargando comentarios...