library(popbio)
?popbio
data(“aq.trans”)#cargar datos aq.trans <- rm(aq.trans)#crear objeto de trabajo
dat\(status <- factor(dat\)stage, levels(factor(dat$stage))[1:4]) attach(dat) head(dat,5)# muestra las primeras 5 filas de datos
(pop <- table(year,status)) ################################### tablaVida <- function(x.sup,Sx, mx){
# NO TOCAR=== # Configuracionesy calculos más avanzados oldOp <- options() digs <- nchar(max(Sx)) options(digits = digs) amplitud <- x.sup - c(0,x.sup[-length(x.sup)]) ex <- numeric(length(Sx) ) for( i in 1:length(Sx) ){ ex[i] <- sum(Sx[i:length(Sx)])/Sx[i]-0.5*amplitud[i] }
Dx <- Sx[-length(Sx)] - Sx[2:length(Sx)] Dx[length(Sx)] <- NA lx <- Sx/Sx[1] dx <- lx[-length(lx)] - lx[2:length(lx)] dx[length(lx)] <- NA qx <- dx/lx mxlx <- mxlx xmxlx <- (x.sup-amplitud/2)mx*lx R0 <- sum(mxlx) Tc <- sum(xmxlx)
#Regresar valores calculados val = data.frame(x = x.sup, S_x = Sx, D_x = Dx, l_x = lx, d_x = dx, q_x = qx, e_x = ex)
colnames(val) <- c(“\(x\)”, “\(S_x\)”, “\(D_x\)”, “\(l_x\)”, “\(d_x\)”, “\(q_x\)”, “\(e_x\)”)
return(list( val, R0 = R0, Tc = Tc
)) options(oldOp) }
Sx <- numeric(4) for(i in 1:4){
Sx[i] <- pop[(i+2),i] } aqData <- tablaVida(x.sup = 1:4, Sx = Sx, mx = 0) aqData <- as.data.frame(aqData) head(aqData)
library(survival)
datos <- data.frame( fecha_nacimiento = as.Date(c(“1929-03-09”, “1900-10-08”, “1945-03-05”, “1927-01-22”, “1920-08-04”, “1958-10-13”, “1948-08-09”, “1934-02-13”, “1937-03-24”, “1957-04-05”, “1960-01-17”, “1926-02-26”, “1923-04-13”, “1895-07-18”, “1976-10-28”, “1917-08-16”, “1935-03-31”, “1926-03-23”, “1959-08-11”, “1981-10-10”, “1930-01-01”, “1932-11-30”, “1936-04-11”, “1912-08-26”, “1940-07-26”, “1913-08-16”, “1936-11-06”, “1907-03-18”, “1938-02-12”, “1923-06-02”, “1926-08-26”, “1910-07-14”, “1921-06-08”, “1933-12-31”, “1937-11-20”, “1941-04-09”, “1937-04-06”, “1974-08-27”, “1917-11-25”, “1987-06-10”, “1939-11-30”, “1938-08-03”, “1943-03-22”, “1914-09-24”, “1949-05-08”, “1915-05-28”, “1913-12-26”, “1933-10-12”, “1940-04-05”, “1910-03-19”, “1937-06-30”, “1939-01-30”, “1958-06-09”, “1933-05-27”, “1963-10-16”, “1942-09-14”, “1966-11-16”, “1909-11-11”, “1964-08-01”, “1926-11-09”, “1946-05-06”, “1916-02-02”, “1923-11-27”, “1959-11-29”, “1957-01-28”, “1957-01-28”, “1962-09-09”, “1918-11-21”, “1933-06-28”, “1969-09-17”, “1930-05-15”, “1952-12-12”, “1970-04-12”, “1922-02-10”, “1912-01-08”, “1922-08-15”, “1890-11-01”, “1925-08-27”, “1925-09-08”, “1910-11-28”, “1944-02-15”, “1892-06-28”, “1893-04-03”, “1918-03-30”, “1914-05-29”, “1955-10-23”, “1931-01-12”, “1923-04-11”, “1961-03-21”, “1934-09-25”, “1893-06-29”, “1956-10-20”, “1899-03-17”, “1954-12-31”, “1929-10-01”, “1919-12-29”, “1906-07-01”, “1921-07-21”, “1937-08-12”, “1970-02-08”, “1951-05-27”, “1991-01-01”, “1924-04-11”, “1925-11-25”, “1935-05-14”, “1937-04-25”, “1999-10-30”, “1917-05-17”, “1955-08-31”, “1916-03-18”, “1914-11-17”, “1985-11-08”, “1975-10-09”, “1967-03-25”, “1974-05-30”, “1928-10-24”, “1982-01-12”, “1972-10-30”, “1990-07-13”, “1933-10-19”, “1944-08-11”, “1995-10-17”, “1948-09-04”, “1956-01-20”, “1936-04-04”, “1948-02-10”, “1951-09-05”, “1894-11-25”, “1907-08-18”, “1969-04-30”, “1938-01-02”, “1970-06-03”, “1905-06-26”, “1973-06-19”, “1968-03-20”, “1958-01-20”, “1976-09-19”, “1969-01-12”, “1918-10-18”, “1929-12-21”, “1900-10-20”, “1920-05-05”, “1902-12-22”, “1968-11-01”, “1982-04-13”, “1932-08-23”, “1967-09-22”, “1927-12-25”, “1946-05-12”, “1942-04-18”, “1927-11-06”, “1899-06-12”, “1930-04-02”, “1925-09-23”, “1927-08-06”, “1927-03-03”, “2008-03-15”, “1922-07-18”, “1926-02-25”, “1977-03-16”, “1930-01-04”, “1947-11-24”, “1949-10-07”, “1933-08-21”, “1939-4-2”, “1930-12-9”, “1932-11-29”, “1978-1-10”, “1921-11-20”, “1994-12-30”, “1997-05-7”, “1955-7-14”, “1947-8-17”, “2014-8-27”, “1939-2-25”, “1915-7-30”, “1918-9-20”, “1947-7-1”, “1893-9-23”, “1933-8-30”, “1930-5-10”, “1915-4-13”, “1971-9-29”, “1957-2-25”, “1956-11-10”, “1956-7-13”, “1958-3-3”, “1945-1-27”, “1968-1-28”, “1937-10-20”, “1966-3-10”, “1954-2-21”, “1922-8-16”, “1946-8-5”, “1981-8-11”, “1938-11-3”, “1939-4-16”, “1922-8-2”, “1954-1-21”, “1941-10-21”, “1942-7-7”, “1964-7-16”, “1962-6-16”, “2004-1-28”, “1968-9-6”, “1960-8-22”, “1988-10-31”, “1924-2-17”, “1939-10-4”, “1948-6-27”, “1978-10-10”, “1931-2-13”, “1931-2-15”, “1955-12-8”, “1962-6-6”, “1968-1-8”, “1954-1-8”, “1944-5-17”, “1932-9-24”, “1951-2-17”, “1931-12-10”, “2004-7-13”, “1961-1-30”, “1945-4-24”, “1949-1-25”, “1942-11-11”, “2014-3-25”, “1929-10-30”, “1953-4-22”, “1987-10-14”, “1930-5-21”, “1925-12-4”, “1922-6-7”, “1945-6-5”, “1949-4-3”, “1940-6-15”, “1939-10-8”, “1926-5-31”, “1993-3-8”, “1934-3-31”, “1924-1-27”, “1936-3-9”, “1967-1-14”, “1934-5-1”, “1957-7-16”, “1956-11-29”, “1926-10-8”, “1928-3-9”, “1937-8-14”, “1929-5-26”, “1952-2-8”, “1960-2-29”, “1927-8-18”, “1927-1-14”, “1969-8-20)), fecha_defuncion = as.Date(c(”2019-07-06”, “1959-11-15”, “2015-12-25”, “2008-08-16”, “198-01-22”, “2019-09-19”, “2015-07-29”, “2015-08-20”, “2001-01-12”, “2005-12-22”, “2010-08-10”, “1996-04-25”, “1937-02-11”, “1992-12-23”, “2018-05-30”, “2013-05-04”, “2006-07-31”, “1993-07-13”, “1980-02-17”, “2011-02-18”, “2007-02-24”, “1996-06-30”, “2004-09-27”, “1949-07-07”, “2025-03-16”, “2005-12-03”, “1963-03-16”, “1986-05-20”, “2022-10-30”, “1994-08-28”, “2006-11-28”, “1995-08-24”, “2016-08-06”, “1997-03-24”, “2024-01-25”, “20114-08-22”, “1997-10-08”, “2020-08-30”, “2009-11-06”, “2003-01-08”, “2018-08-11”, “2025-04-22”, “2020-09-12”, “1989-07-29”, “2018-07-02”, “1988-06-24”, “1970-11-05”, “1986-01-22”, “2018-08-26”, “1995-04-12”, “2021-12-15”, “2003-08-28”, “1988-11-21” , “1980-05-24”, “2019-11-05”, “2021-11-12”, “2019-05-17”, “1971-03-21”, “1978-07-16”, “2009-10-25”, “2008-05-07”, “,”1983-09-02”, “2011-12-19”, “1997-09-11”, “2014-04-15”, “2021-05-07”, “2024-06-09”, “2000-01-17”, “,”1993-12-26”, “2004-06-28”, “2021-12-04”, “2003-02-21”, “2015-10-23”, “1995-05-20”, “1995-02-03”, “1993-06-23”, “1982-08-27”, “2000-12-27”, “2006-07-18”, “1986-02-13”, “2021-01-09”, “1960-11-6”, “1963-08-05”, “1994-05-06”, “1998-09-09”, “1976-01-25”, “2006-05-09”, “2005-08-25”, “1979-05-25”, “2014-09-04”, “1988-11-05”, “2002-03-07”, “1945-11-09”, “1996-11-19”, “2013-10-17”, “2016-08-27”, “1968-08-28”, “1985-12-20”, “1997-10-25”, “2017-10-20”, “2005-01-25”, “2029-06-29”, “2018-07-26”, “2012-01-16”, “1997-12-23”, “2013-01-18”, “1999-07-25”, “2001-11-13”, “2002-05-25”, “1992-02-21”, “2001-02-23”, “2006-03-18”, “2020-08-05”, “1969-09-03”, “2014-02-12”, “2004-12-01”, “2014-08-18”, “2002-04-28”, “2023-09-20”, “2012-10-29”, “2010-12-02”, “2021-10-07”, “2014-06-20”, “2007-11-28”, “2019-12-05”, “2000-05-27”, “2018-08-11”, “1958-05-01”, “1987-08-27”, “2005-01-04”, “2003-01-02”, “2014-05-20”, “1988-06-17”, “2023-02-27”, “2007-07-28”, “2019-11-21”, “1996-01-02”, “2019-01-08”, “2016-07-02”, “2005-01-01”, “1942-09-30”, “2009-08-15”, “1981-03-20”, “2012-01-03”, “2018-02-20”, “2004-10-17”, “2013-05-26”, “1943-03-07”, “2001-05-05”, “2013-07-21”, “1963-09-24”, “1973-07-17”, “1944-02-28”, “2015-07-23”, “2019-10-18”, “1948-11-15”, “2008-10-21”, “2001-02-20”, “2007-07-18”, “1994-07-12”, “2011-11-17”, “2014-11-18”, “2024-04-24”, “2011-05-29”, “2016-10-19”, “2018-2-2”, “1986-5-11”, “2011-11-13”, “2004-2-24”, “1995-08-22”, “2001-9-20”, “2019-10-30”, “2019-10-17”, “2023-2-28”, “1992-1-22”, “2004-8-19”, “2009-6-24”, “2016-7-7”, “1976-9-21”, “2009-7-9”, “2000-12-17”, “1988-10-26”, “2025-8-31”, “2019-5-11”, “2020-7-23”,“,”2025-3-19”, “2020-6-5”, “2021-3-22”, “2021-3-17”, “2021-2-3”, “2021-6-8”, “2021-5-5”, “2021-9-19”, “2021-9-29”, “2021-7-3”, “2022-3-24”, “2022-2-11”, “2022-2-17”, “2022-2-10”, “2022-7-3”, “2022-6-28”, “2022-10-7”, “2023-2-20”, “2023-9-14”, “2023-7-8”, “2023-12-6”, “2023-11-8”, “2003-9-10”, “2017-9-16” “2017-10-15”, “2017-6-12”, “2017-9-25”, “2017-6-19”, “2015-11-8”, “2017-12-6”, “2017-9-30”, “2017-8-18”, “2023-3-19”, “2023-11-19”, “2017-7-2”, “2011-12-4”, “2022-7-24”, “2017-7-17”, “2022-9-15”, “2017-8-21”, “2013-6-30”, “2014-3-30”, “2005-11-24”, “2012-11-22”, “2020-9-3”, “2020-9-3”, “2015-1-7”, “2014-6-26”, “2017-3-22”, “2012-8-28”, “2012-9-14”, “2012-8-8”, “2015-5-16”, “2017-6-10”, “2015-1-29”, “2019-3-7”, “2013-2-14”, “2013-2-8”, “2020-12-11”, “2020-11-7”, “2013-2-5”, “1010-11-4”, “2020-11-2”, “2015-4-21”, “2013-1-31”, “2015-10-4”, “2015-9-11”, “2013-3-14”, “2015-11-9”, “2021-2-5”, NA)) # NA para personas vivas ) View(datos)
datos\(edad_muerte <- ifelse(is.na(datos\)fecha_defuncion), as.numeric(difftime(Sys.Date(), datos\(fecha_nacimiento, units = "days")), as.numeric(difftime(datos\)fecha_defuncion, datos$fecha_nacimiento, units = “days”)))
supervivencia <- Surv(time = datos\(edad_muerte, event = !is.na(datos\)fecha_defuncion))
km_fit <- survfit(supervivencia ~ 1)
summary(km_fit)
plot(km_fit, main = “Curva de Supervivencia”) #####################
tabla_vida
tabla_vida\(edad_muerte <- ifelse(is.na(tabla_vida\)Muerte), as.numeric(difftime(Sys.Date(), tabla_vida\(Nacimiento, units = "days")), as.numeric(difftime(tabla_vida\)Muerte, tabla_vida$Nacimiento, units = “days”)))
supervivencia <- Surv(time = tabla_vida\(edad_muerte/365, event = !is.na(tabla_vida\)Muerte))
km_fit <- survfit(supervivencia ~ 1)
plot(km_fit, main = “Curva de Supervivencia”)
library(dplyr)
tabla