Bioestadistica Co R Avanzada 1
Bioestadistica Co R Avanzada 1
BIOESTADÍSTICA BÁSICA
Y AVANZADA CON
«No está permitida la reproducción total o parcial de este libro, ni su tratamiento informá-
tico, ni la transmisión de ninguna forma o por cualquier
medio, ya sea electrónico, mecánico por fotocopia, por registro u otros métodos, sin el
permiso previo y por escrito de los titulares del Copyright.»
DEDICATORIA......................................................................................VII
PREFACIO............................................................................................. XI
CONJUNTO DE DATOS........................................................................... XV
PARTE I.
Bioestadística básica
1. Análisis descriptivo.........................................................................3
2. Análisis bivariante.........................................................................15
3. Análisis multivariante....................................................................51
-IX-
X BIOESTADÍSTICA BÁSICA Y AVANZADA CON R
PARTE II.
Bioestadística avanzada
4. Modelos Lineales Generalizados (GLM) ........................................139
ANEXOS............................................................................................. 235
Contexto de aplicación
Esta obra es el fruto de más de 20 años de dedicación profesional al
análisis de datos en Ciencias de la Salud, mostrando soluciones sencillas
mediante código R directo, a problemas reales de investigación traslacio-
nal e investigacion aplicada en Biomedicina.
La investigación traslacional es la investigación que traslada los cono-
cimientos de la investigación básica a las personas. Por tanto, las unidades
de análisis serán personas o pacientes, en contextos de estudios epidemio-
lógicos o clínicos.
La bioestadística es la parte de análisis de datos dentro del proceso in-
vestigador, pero antes del análisis de datos, es necesario haber diseñado un
-XI-
XII BIOESTADÍSTICA BÁSICA Y AVANZADA CON R
Métodos
En bioestadística pueden existir distintos caminos para resolver un mis-
mo objetivo, lo importante es que los métodos aplicados sean correctos
en cada caso. En este libro se aplican métodos estándar ampliamente
utilizados en la literatura estadística clásica, mostrando la referencia del
autor original en cada caso.
Hay aspectos estadísticos que aparecen en las guías de presentación
de resultados en investigaciones biomédicas, como SAMPL [3], que po-
drían ser algo discutibles:
Por ejemplo, comprobar previamente hipótesis como normalidad o
homogeneidad de varianzas antes de realizar un test de comparación de
medias, aumenta el error tipo I global [4].
Establecer previamente el “nivel de significatividad” en 0.05 para “deci-
dir” si un test es significativo o no lo es, es ciertamente una falacia [5]. No
hay una frontera a partir de la cual, una investigacion prorporciona eviden-
cia de si un tratamiento es eficaz o no, y lo que es más importante, el inves-
tigador no debe “decidir” si es eficaz o no, solo mostrar grados de evidencia,
en forma de incertidumbre, sobre la pregunta de investigación planteada.
Prefacio XIII
Nomenclaturas
A lo largo de este estudio, a la variable dependiente se la llamará variable
respuesta, variable resultado o evento de interés.
Las variables independientes se podrán llamar explicativas, predictores
o factores. Según la naturaleza de las variables, llamaremos cuantitativas o
continuas por un lado, o cualitativas, factores o categóricas por otro lado,
tomándolos como términos sinónimos en la práctica.
Conjunto de datos
DATASET PACKAGE
Pima.tr MASS
Pima.te
Diabetes in Pima Indian Women. A population of women who were at least 21 years
old, of Pima Indian heritage and living near Phoenix, Arizona, was tested for diabetes
according to World Health Organization criteria. The data were collected by the US
National Institute of Diabetes and Digestive and Kidney Diseases. We used the 532
complete records after dropping the (mainly missing) data on serum insulin (Pima.tr
contains a randomly selected set of 200 subjects, and Pima.te contains the remaining
332 subjects)
Variables and values
• glu: plasma glucose concentration in an oral glucose tolerance test.
• bp:diastolic blood pressure (mm Hg).
• skin:triceps skin fold thickness (mm).
• bmi:body mass index (weight in kg/(height in m)\^2).
• ped:diabetes pedigree function.
• age:age in years.
• type:Yes or No, for diabetic according to WHO criteria.
• glu: plasma glucose concentration in an oral glucose tolerance test.
• bp:diastolic blood pressure (mm Hg).
• skin:triceps skin fold thickness (mm).
-XV-
XVI BIOESTADÍSTICA BÁSICA Y AVANZADA CON R
DATASET PACKAGE
pharmacoSmoking asaur
Randomized trial of triple therapy vs. patch for smoking cessation. 125 patients and
14 variables.
Variables and values
• id: patient ID number
• ttr: Time in days until relapse
• relapse: Indicator of relapse (return to smoking)
• grp: Randomly assigned treatment group with levels combination or patchOnly
• age: Age in years at time of randomization
• gender: Female or Male
• race: black, hispanic, white, or other
• employment: ft (full-time), pt (part-time), or other
• yearsSmoking: Number of years the patient had been a smoker
• levelSmoking: heavy or light
DATASET PACKAGE
prostate genridge
Data to examine the correlation between the level of prostate-specific antigen (PSA)
and a number of clinical measures in men who were about to receive a radical
prostatectomy. A dataframe with 97 observations on the following 10 variables.
Variables and values
• lcavol: log cancer volume
• lweight: log prostate weight
• age: in years
• lbph: log of the amount of benign prostatic hyperplasia
• svi: seminal vesicle invasión (0=No; 1=Yes)
• lcp: log of capsular penetration
• gleason: Gleason Score: a numeric vector
• pgg45: percent of Gleason score 4 or 5
• lpsa: response PSA
• train: a logical vector
Conjunto de datos XVII
DATASET PACKAGE
covid_testing medicaldata
This data set is from Amrom E. Obstfeld, who de-identified data on COVID-19 testing
during the year 2020 at the Children’s Hospital of Pennsylvania (aka CHOP). This data
set contains data concerning testing for SARS-CoV2 via PCR as well as associated
metadata. The data has been anonymized, time-shifted, and permuted.
The COVID-19 pandemic in 2020 required rapid development and deployment of a
novel PCR test, and application to many outpatients with symptoms, and all inpatients
upon admission to the emergency room or the hospital. These anonymized data (with
fake subject_ids, names, ages, and genders) provide a snapshot of the first ~100 days of
testing at a single pediatric hospital, with over 15,000 tests performed on patients in a
variety of settings. The vast majority were a single type of test. These tests occurred in
88 named clinics (clinic_name) and in 5 demographic groups (demo_group). Roughly
half of the tests were performed at drive-through sites (drive_thru_ind).
The test result (result) can be either negative, positive, or invalid. The ct_result provides
the cycle number at which the positive threshold was reached during PCR. Lower
integer values for ct_result occur with a higher viral load (higher number of viral
particles) in the test sample (deep nasal swab).
The test order can have been placed as a one-off order, or within an order set. The
payor_group for an ordered test can be one of 7 categories (includes insurance type
and self-pay). The patients are categorized into 9 patient classes, including inpatient,
emergency, outpatient, etc. The col_rec_tat variable records the time elapsed (in hours)
between sample collection and receipt in the lab. The rec__ver_tat variable records the
time elapsed (in hours) between sample receipt in the lab and result verification/posting.
Variables and values
DATASET PACKAGE
AlzheimerDisease AppliedPredictiveModeling
Washington University conducted a clinical study to determine if biological
measurements made from cerebrospinal fluid (CSF) can be used to diagnose or
predict Alzheimer’s disease (Craig-Schapiro et al. 2011). These data are a modified
version of the values used for the publication. The R factor vector diagnosis contains
the outcome data for 333 of the subjects. The demographic and laboratory results are
collected in the dataframe predictors.
Variables and values
• diagnosis: labels for the patients, either “Impaired” or “Control”.
• predictors: predictors for demographic data (eg. age, gender), genotype and
assay results.
Explanatory variables of interest in predictors
o age
o male: 0=woman, 1=men
o tau, p_tau, Ab_42: 3 known proteins (numeric)
Conjunto de datos XIX
DATASET PACKAGE
smartpill medicaldata
Prospective Cohort Study of Intestinal Transit using a SmartPill to Compare Trauma
Patients to Healthy Volunteers This study evaluated gastric emptying, small bowel
transit time, and total intestinal transit time in 8 critically ill trauma patients. These
data were compared with those obtained in 87 healthy volunteers from a separate trial.
Data were obtained with a motility capsule that wirelessly transmitted pH, pressure,
and temperature to a recorder attached to each subject’s abdomen. Transit times were
available for almost all patients, however, pH, pressure and temperature data is missing
for all critically ill patients and sparsely missing for the healthy volunteers
Variables and values
• Group Study group, numeric, 0 = Critically Ill Trama Patient, 1 = Healthy Vol-
unteer
• Gender Gender, numeric, range: 0 = Female, 1 = Male
• Race Race, numeric, 1 = White, 2 = Black, 3 = Asian/Pacific Islander, 4 = His-
panic, 5 = Other
• Height Height (centimeters), numeric, range: 132.1-193.0
• Weight Weight (kilograms), numeric, range: 44.9-127.0
• Age Age (years), numeric, range: 18.0-72.0
• GE.Time Gastric Emptying Time is time from ingestion to gastric emptying
(hours), numeric, range: 1.7-74.3
• SB.Time Small Bowel Transit Time is time from gastric emptying to ileocecal
junction (hours), numeric, range: 1.8-13.8
• C.Time Colonic Transit Time is time from ileocecal junction to body exit
(hours), numeric, range: 0.7-118.9
• WG.Time Whole Gut Time is time from ingestion to body exit (hours), numeric,
range: 6.0-816.0
• S.Contractions Stomach contractions are counted if the peak amplitude of
the contraction is over 10 mmHg and under 300 mmHg, numeric, range: 47.0-
1665.0
• .Sum.of.Amplitudes Stomach sum of amplitudes (mm Hg), numeric, range:
655.6-33800.3
• S.Mean.Peak.Amplitude Stomach mean peak amplitude is the sum of am-
plitudes divided by number of contractions (mm Hg), numeric, range: 4.6-43.4
• S.Mean.pH Stomach mean pH is the average pH over the whole recording time
in the stomach with normal ~ 1.5-3.5, numeric, range: 1.5-5.9
• SB.Contractions Small Bowel contractions are counted if the peak ampli-
tude of the contraction is over 10 mmHg and under 300 mmHg, numeric, range:
223.0-2375.0
• SB.Sum.of.Amplitudes Small Bowel sum of amplitudes (mm Hg), numer-
ic, range:3899.4-41122.5
XX BIOESTADÍSTICA BÁSICA Y AVANZADA CON R
DATASET PACKAGE
supraclavicular medicaldata
This data set contains 103 patients who were scheduled to undergo an upper
extremity procedure suitable for supraclavicular anesthesia. Patients were randomly
assigned to either (1) combined group-ropivacaine and mepivacaine mixture; or (2)
sequential group-mepivacaine followed by ropivacaine. A number of demographic
and post-op pain medication variables (fentanyl, alfentanil, midazolam) were
collected. The primary outcome is time to 4-nerve sensory block onset. The dataset is
cleaned and relatively complete. There are no outliers or data problems.
Variables and values
• subject Subject ID, numeric, range: 1-103
• group Anesthetic group, numeric, 1 = Mixture; 2 = Sequential
• gender Gender, numeric, 1 = Male; 0= Female
• bmi Body mass index (kg/m^2), numeric, range:19-43.5
• age Age (years), numeric, range:18-74
• fentanyl Fentanyl pain medication (micrograms), numeric, range: 0-250.0
• alfentanil Alfentanil pain medication (milligrams), numeric, range: 0-4.3
• midazolam Midazolam hypnotic-sedative medication, numeric, range: 0-9.0
• onset_sensory Time to 4 nerve sensory block onset or, if onset_sensory
block failed the observed worst outcome of minutes for any patient (50 minutes),
numeric, range: 0-50.0
• onset_first_sensory Time to first sensory block in minutes, or if block
failed, a value of 15 minutes, numeric, range: 6-15.0
Conjunto de datos XXI
• onset_motor Time to complete motor block or, if motor block failed, the ob-
served worst outcome of minutes for any patient (50 minutes), numeric, range:
1-50.0
• nerve_block_censor block failed, numeric, 0 = nerve block succeeded, 1 =
block failed (censored)
• med_duration Time from the onset of 4 nerve sensory block until the first
request for an analgesic medication (hours), numeric, range: 0-48.0
• med_censor Patients who did not take an analgesic were censored at 48 hours,
numeric, 0 = nerve succeeded, 1 = block failed (censored)
• vps_rest Maximum postop verbal pain score (at rest), on 11 point Likert scale
(0-10), numeric, range: 0-10
• vps_movement Maximum postop verbal pain score (with movement), on 11
point Likert scale (0- 10), numeric, range: 0-10
• opioid_total Total opioid consumption in milligrams, numeric, range:
0-225.0
DATASET PACKAGE
esoph_ca medicaldata
Data from a case-control study of esophageal cancer in Ille-et-Vilaine, France,
evaluating the effects of smoking and alcohol on the incidence of esophageal cancer.
Smoking and alcohol are associated risk factors for squamous cell cancer of the
esophagus, rather than adenocarcinoma of the esophagus, which is associated with
obesity and esophageal reflux (more details available below the variable definitions).
A dataframe with 88 rows and 5 variables, with 200 cases and 975 controls.
Variables and values
• agegp 6 levels of age: “25-34”, “35-44”, “45-54”, “55-64”, “65-74”, “75+”;
type: ordinal factor
• alcgp 4 levels of alcohol consumption: “0-39g/day”, “40-79”, “80-119”,
“120+”; type: ordinal factor
• tobgp 4 levels of tobacco consumption: “0-9g/day”, “10-19”, ’20-29”, “30+”;
type: ordinal factor
• ncases Number of cases; type: integer
• ncontrols Number of controls; type: integer
XXII BIOESTADÍSTICA BÁSICA Y AVANZADA CON R
DATASET PACKAGE
cancer Survival
Survival in patients with advanced lung cancer from the North Central Cancer
Treatment Group. Performance scores rate how well the patient can perform usual
daily activities.
Variables and values
• inst: Institution code
• time: Survival time in days
• status: censoring status 1=censored, 2=dead
• age: Age in years
• sex: Male=1 Female=2
• ph.ecog: ECOG performance score as rated by the physician. 0=asymptomatic,
1= symptomatic but completely ambulatory, 2= in bed <50% of the day, 3= in
bed > 50% of the day but not bedbound, 4 = bedbound
• ph.karno: Karnofsky performance score (bad=0-good=100) rated by physician
• meal.cal: Calories consumed at meals
• wt.loss: Weight loss in last six months
PARTE I
Bioestadística básica
1
Análisis descriptivo
-3-
4 BIOESTADÍSTICA BÁSICA Y AVANZADA CON R
∞∞∞∞∞∞∞∞∞
Análisis descriptivo gráfico con R
En un análisis de datos, lo primero es echar un vistazo a los datos me-
diante graficas univariantes. Así nos podemos hacer una idea de la forma
y estructura de los datos.
Mediante el siguiente sencillo código se calculan gráficos de barras
para las variables cualitativas (factores) mediante barplot()e histo-
gramas para las cuantitativas mediante hist().
Pasamos un bucle for que recorre las variables y va poniendo los
gráficos en ventanas nuevas mediante win.graph().
Debemos poner el nombre de la base de datos en my_dataset
par(mfrow = c(3,3))
for(j in 1:length(vari)){
if(is.factor(dataset[,j])) {
} else {
p <- round(lillie.test(dataset[, j])$p.value,3)
hist(dataset[, j], freq=F, ylab="Frencuency", xlab=vari[j],
main=paste("Normality p-value = ",p))
}
}
∞∞∞∞∞∞∞∞∞
6 BIOESTADÍSTICA BÁSICA Y AVANZADA CON R
> names(prostate)
[1] “lcavol” “lweight” “age” “lbph” “svi” “lcp”
“gleason” “pgg45” “lpsa” “train”
> class(dataset$svi)
[1] “integer”
> table(dataset$svi)
No Yes
76 21
∞∞∞∞∞∞∞∞∞
Cálculo de estadísticos descriptivos con R
El primer análisis en una investigación de este tipo es un análisis descrip-
tivo de todas las variables. Dependiendo de la naturaleza de las variables
se calcula unos estadísticos u otros. De esta manera, para las variables
cualitativas, se suele calcular el número y porcentaje. Para las variables
cuantitativas se puede calcular el n valido (si hay missings en alguna va-
riable), el valor mínimo, máximo, medio y desviación estándar para va-
riables aproximadamente Gaussianas. Para distribuciones asimétricas se
puede calcular la mediana y el rango intercuartílico (Tabla 1.1).
Variables cualitativas
Número na
Porcentaje (na*100/n)%
Variables cuantitativas
n valido n without missings
mínimo Min(xi), i=1,…n
máximo Max(xi), i=1,…n
Media Σxi/n , i=1,…n
Desviación estándar Σ(xi-mean)^2/(n-1)
Mediana (n+1)/2
Rango intercuartilico x(n+1)75/n ; x(n+1)75/n
nvalido = sum(!is.na(var))
10 BIOESTADÍSTICA BÁSICA Y AVANZADA CON R
library(plyr)
if(is.factor(dataset [,name.var])) {
out[[name.var]] <- descrip.cat(dataset[, name.var])
} else {
Solo falta una cosa: el código anterior añade los nombres de la lis-
ta out al dataframe out.cat, pero los repite para cada variable. Para
eliminar los nombres de las variables repetidos aplicamos el siguiente
código:
out.cat$var[i] <- “”
} else {
}
}
library(officer)
library(flextable)
library(magrittr)
my.table.style = function(x){
std_b = fp_border(color=”black”)
cuali.t <- regulartable(x)
cuali.t <- set_formatter_type(cuali.t, fmt_double = “%.01f”)
cuali.t <- fontsize(cuali.t, size=10)
cuali.t <- font(cuali.t, fontname = “Calibri”)
cuali.t <- align(cuali.t, align=”left”, j=1)
cuali.t <- align(cuali.t, align=”left”, j=2)
cuali.t <- border_remove(cuali.t)
cuali.t <- hline(cuali.t, border = std_b, part=”header”)
cuali.t <- hline_top(cuali.t, border = std_b, part=”all”)
cuali.t <- hline_bottom(cuali.t, border = std_b, part=”all”)
cuali.t <- width(cuali.t, width = 1.4)
}
También se podrían tener dos funciones, una para variables tipo factor
y otra para variables cuantitativas, por si nos interesa, por ejemplo, poner
más decimales a los descriptivos de las cuantitativas. Se puede consultar
la ayuda de flextable para modificar estos parámetros.
Cargamos la función con
source(“table_style.r”)
library (plyr)
∞∞∞∞∞∞∞∞∞
Ejemplo 1.2. Análisis descriptivo
Siguiendo con la base de datos prostate analizada en el Ejemplo 1.1.,
aplicamos las funciones al objeto dataset, donde ya depuramos y con-
vertimos a factor la variable svi.
El resultado se guardar en un Word llamado descriptives.
docx con el resultado mostrado en la Tabla 1.2:
14 BIOESTADÍSTICA BÁSICA Y AVANZADA CON R
Factor variables
.id cat n p
svi No 76 78.4
Yes 21 21.6
Quantitative variables
.id nvalid Min Max Mean SD Median IQR
lcavol 97 -1.3 3.8 1.4 1.2 1.4 1.6
lweight 97 2.4 4.8 3.6 0.4 3.6 0.5
age 97 41.0 79.0 63.9 7.4 65.0 8.0
lbph 97 -1.4 2.3 0.1 1.4 0.3 2.9
lcp 97 -1.4 2.9 -0.2 1.4 -0.8 2.6
gleason 97 6.0 9.0 6.8 0.7 7.0 1.0
pgg45 97 0.0 100.0 24.4 28.2 15.0 40.0
lpsa 97 -0.4 5.6 2.5 1.1 2.6 1.3