8  Escalas

8.1 Escalas

Nos basaremos en el paquete de obtención de cuadros y tablas.

Escala básica

pacman::p_load(tidyr,kableExtra,expss)
data <- read_spss("data/ejemplogbw8.sav")
# si hay ponderación
#data <- weight_by(data, P16)

8.1.1 Absolutos

digits=NULL
data %>% 
     tab_cols('|'=unvr(P6A)) %>% 
     tab_stat_cases(label=var_lab(data$P6A),total_row_position = 'none') %>% 
     tab_cols('|'=unvr(P6B)) %>% 
     tab_stat_cases(label=var_lab(data$P6B),total_row_position = 'none') %>% 
     tab_cols('|'=unvr(P6C)) %>% 
     tab_stat_cases(label=var_lab(data$P6C),total_row_position = 'none') %>% 
     tab_cols('|'=unvr(P6D)) %>% 
     tab_stat_cases(label=var_lab(data$P6D),total_row_position = 'none') %>% 
     tab_pivot()
 montaña   costa/playa   ciudad   campo 
 #Total 
   Primer lugar elegido  160 96 72 56
   Segundo lugar elegido  80 144 64 96
   Tercer lugar elegido  64 132 52 136
   Cuarto lugar elegido  80 12 196 96
#total_col <- rowSums(as.data.frame(tab[,-1]))
#total_rows <- colSums(as.data.frame(tab[,-1]))
#total_col
#total_rows

8.1.2 Porcentajes

expss_digits(digits=1)
data %>% 
     tab_cols('|'=unvr(P6A)) %>% 
     tab_stat_rpct(label=var_lab(data$P6A),total_row_position = 'none') %>% 
     tab_cols('|'=unvr(P6B)) %>% 
     tab_stat_rpct(label=var_lab(data$P6B),total_row_position = 'none') %>% 
     tab_cols('|'=unvr(P6C)) %>% 
     tab_stat_rpct(label=var_lab(data$P6C),total_row_position = 'none') %>% 
     tab_cols('|'=unvr(P6D)) %>% 
     tab_stat_rpct(label=var_lab(data$P6D),total_row_position = 'none') %>% 
     tab_pivot()
 montaña   costa/playa   ciudad   campo 
 #Total 
   Primer lugar elegido  41.7 25 18.8 14.6
   Segundo lugar elegido  20.8 37.5 16.7 25
   Tercer lugar elegido  16.7 34.4 13.5 35.4
   Cuarto lugar elegido  20.8 3.1 51 25
expss_digits(digits=NULL)

8.1.3 Otros

expss_digits(digits=2)
data %>% 
    tab_cells(P6A,P6B,P6C,P6D) %>%
    tab_cols(total(label = "|")) %>% 
    tab_stat_fun(
        "Mean" = w_mean,
        "Std. dev." = w_sd,
        "Valid N" = w_n,
        "Maximo" = w_max,
        "Mínimo" = w_min,
        "Variance" = w_var,
        "Median" = w_median,
        "Mad" = w_mad,
        "Sum" = w_sum,
        method = list
    ) %>% 
    tab_pivot()
 Mean   Std. dev.   Valid N   Maximo   Mínimo   Variance   Median   Mad   Sum 
 Primer lugar elegido  2.06 1.09 384 4 1 1.19 2 1.48 792
 Segundo lugar elegido  2.46 1.08 384 4 1 1.17 2 1.48 944
 Tercer lugar elegido  2.68 1.12 384 4 1 1.26 2 1.48 1028
 Cuarto lugar elegido  2.80 1.04 384 4 1 1.08 3 0.00 1076
expss_digits(digits=NULL)

8.2 > No tener en cuenta, pruebas

pacman::p_load(tidyr,kableExtra,expss)
data <- read_spss("data/ejemplogbw8.sav")

data <- maditr::columns(unlab(data), P6A,P6B,P6C,P6D)
readr::write_csv(data, "~/R/r-projects/19.r-brb/exampleP6.csv")
Error:
! Cannot open file for writing:
* 'C:/Users/rober/Documents/R/r-projects/19.r-brb/exampleP6.csv'
df <- readr::read_csv("exampleP6.csv")

val_lab(df[c("P6A", "P6B", "P6C", "P6D")]) <- c("Mountains"=1, "Beach"=2, "City"=3, "Cottage / rural"=4)

var_lab(df$P6A) <- 'First option'
var_lab(df$P6B) <- 'Second option'
var_lab(df$P6C) <- 'Third option'
var_lab(df$P6D) <- 'Fourth option'

tab <- df %>% 
     tab_cols('|'=unvr(P6A)) %>% 
     tab_stat_rpct(label=var_lab(df$P6A),total_row_position = 'none') %>% 
     tab_cols('|'=unvr(P6B)) %>% 
     tab_stat_rpct(label=var_lab(df$P6B),total_row_position = 'none') %>% 
     tab_cols('|'=unvr(P6C)) %>% 
     tab_stat_rpct(label=var_lab(df$P6C),total_row_position = 'none') %>% 
     tab_cols('|'=unvr(P6D)) %>% 
     tab_stat_rpct(label=var_lab(df$P6D),total_row_position = 'none') %>% 
     tab_pivot() %>% 
    as.data.frame()
tab <- data %>% 
    tab_cols('|'=unvr(P6A)) %>% 
    tab_stat_rpct(label=var_lab(data$P6A),total_row_position = 'none') %>% 
    tab_cols('|'=unvr(P6B)) %>% 
    tab_stat_rpct(label=var_lab(data$P6B),total_row_position = 'none') %>% 
    tab_cols('|'=unvr(P6C)) %>% 
    tab_stat_rpct(label=var_lab(data$P6C),total_row_position = 'none') %>% 
    tab_cols('|'=unvr(P6D)) %>% 
    tab_stat_rpct(label=var_lab(data$P6D),total_row_position = 'none') %>% 
    tab_stat_fun_df(' |#Total cases'=function(x) {
        nrow(data)}
        ) %>%
    tab_pivot()
tab <- split_columns(tab, columns=1, split="\\|")
tab <- as.etable(tab[,-1])
significance_cpct(tab)
 1     2     3     4 
 A     B     C     D 
41.7 B C D   25.0 C D     18.8     14.6  
20.8     37.5 A C D   16.7     25.0 C  
16.7     34.4 A C     13.5     35.4 A C
20.8 B     3.1     51.0 A B D   25.0 B  
 #Total cases  384     384     384     384  
tab1 <- data %>% 
    tab_cols(total(label="|")) %>% 
    tab_cells('|'=unvr(P6A)) %>% 
    tab_stat_cpct(label=var_lab(data$P6A),total_row_position = 'below') %>% 
    tab_cells('|'=unvr(P6B)) %>% 
    tab_stat_cpct(label=var_lab(data$P6B),total_row_position = 'below') %>% 
    tab_last_hstack() %>% 
    tab_cells('|'=unvr(P6C)) %>% 
    tab_stat_cpct(label=var_lab(data$P6C),total_row_position = 'below') %>% 
    tab_last_hstack() %>% 
    tab_cells('|'=unvr(P6D)) %>% 
    tab_stat_cpct(label=var_lab(data$P6D),total_row_position = 'below') %>% 
    tab_last_hstack() %>% 
    tab_pivot()

#tab1 <- split_columns(tab, columns=1, split="\\|")
#tab1 <- as.etable(tab1[,-1])
t(significance_cpct(tab1))
 1   2   3   4   #Total cases 
 A  41.7 B C D 25.0 D   18.8 C   14.6   384  
 B  20.8   37.5 A D 16.7   25.0 A   384  
 C  16.7   34.4 A D   13.5   35.4 A B D 384  
 D  20.8   3.1   51.0 A B C 25.0 A   384