Skip to content

Instantly share code, notes, and snippets.

@avallecam
Last active December 15, 2018 16:47
Show Gist options
  • Save avallecam/ae211fc1639d96f6bc56ed0a47415f77 to your computer and use it in GitHub Desktop.
Save avallecam/ae211fc1639d96f6bc56ed0a47415f77 to your computer and use it in GitHub Desktop.
STATA: Genera descritivos rápidos con bases de datos gigantes!
library(tidyverse)
library(haven)
library(labelled)
#crear data frame
set.seed(22)
dta <- data_frame("edad" = floor(rnorm(6,25,10)),
"peso" = floor(rnorm(6,60,10)),
"sexo" = gl(2,1,6, labels = c("f","m")),
"altura" = gl(2,2,6, labels = c("bajo","alto")),
"outcome" = rep(c("primaria","secundaria","universidad"),each=2))
#regla
!grepl("^[A-Za-z_]{1}[A-Za-z0-9_]{0,31}$", names(dta))
#fuente:
#https://github.com/tidyverse/haven/blob/master/R/haven.R
#con error
dta %>%
mutate_if(is.character,as.factor) %>%
write_dta("data/test.dta")
*importar base de datos
use "data/test.dta"
*ver los tipos de variable
describe
*crear un "vector/lista" de variables según el tipo
*ejecutar un loop con funciones descriptivas
*la línea if+condition+continue permite evitar ciertas variables dentro de la lista creada
*loop para variables categóricas (guardadas con tipo long)
ds, has(type long)
foreach var in `r(varlist)' {
if "`var'" == "outcome" continue
codebook `var', detail
tab `var' outcome, chi col
}
*loop para variables numéricas (guardadas con tipo double)
ds, has(type double)
foreach var in `r(varlist)' {
codebook `var', detail
tabstat `var', s(n min max mean sd q sk k) by(outcome)
oneway `var' outcome, bonferroni
*kwallis2 `var', by(community1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment