### Install IrregLong from GitHub Source: https://github.com/epullenayegum/irreglong/blob/master/README.md Install the development version of the IrregLong package using devtools. ```r devtools::install_github("epullenayegum/Irreglong") ``` -------------------------------- ### Setup R Markdown Chunk Source: https://github.com/epullenayegum/irreglong/blob/master/cran-comments.md This chunk is used for setting up R Markdown options. It is typically included at the beginning of an R Markdown document. ```r knitr::opts_chunk$set(echo = TRUE) ``` -------------------------------- ### Data Preparation for Irregular Longitudinal Analysis Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html Prepares the Phenobarb dataset for irregular longitudinal analysis by creating an 'event' variable, filtering data, assigning subject IDs, and restricting time to the first 16 days. This setup is crucial for subsequent GEE modeling. ```r library(IrregLong) library(MEMSS) library(survival) library(geeM) library(data.table) data(Phenobarb) Phenobarb$event <- 1-as.numeric(is.na(Phenobarb$conc)) data <- Phenobarb data <- data[data$event==1,] data$id <- as.numeric(data$Subject) data <- data[data$time<16*24,] data <- data[order(data$id,data$time),] head(data) ``` -------------------------------- ### Model Assessment Intensity with iiw.weights Source: https://github.com/epullenayegum/irreglong/blob/master/README.md Model the assessment intensity using a proportional rates model with the iiw.weights function. This example uses data from the MEMSS package to check for evidence against the ACAR assumption by examining the association between serum concentration and subsequent assessment intensity. ```r i <- iiw.weights(Surv(time.lag,time,event)~I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30) + cluster(Subject),id="Subject",time="time",event="event",data=data, invariant=c("Subject","Wt"),lagvars=c("time","conc"),maxfu=16*24,lagfirst=c(0,0),first=FALSE) i$m ``` -------------------------------- ### Fit Irregular Longitudinal GEE Model Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html This snippet shows how to fit a generalized estimating equation model for irregularly spaced longitudinal data using the `iiwgee` function. Ensure the `irreglong` package is installed and loaded. ```r library(irreglong) # Assuming 'data' is your data frame with appropriate columns for subject, time, and outcome # and 'formula' defines the model structure. # Example: formula <- outcome ~ predictor1 + predictor2 # Fit the model iiwgee <- iiwgee(formula, data = data, id = data$subject, waves = data$time, family = gaussian, corstr = "exchangeable") ``` -------------------------------- ### Define Time-Dependent Covariate Function (Wfn) Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Defines the time-dependent covariates for the Liang model. This example includes an intercept and the time variable itself. ```R Wfn <- function(id,time){ return(c(1,time)) } ``` -------------------------------- ### Covariate Function for Time-Invariant Group Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html Extracts the first value of the 'ApgarInd' covariate for a given subject, assuming it is time-invariant. This function is used within the Liang model setup. ```R Xfn <- function(id,time){ # Group is time invariant so just use the first value for each subject return(as.numeric(data$ApgarInd[data$id==id][1])) } ``` -------------------------------- ### Load Libraries and Data for IrregLong Analysis Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Loads necessary R packages and the Phenobarb dataset. It then preprocesses the data by creating an event indicator, filtering for relevant observations, creating a subject ID, and restricting the time frame. ```R library(IrregLong) library(MEMSS) library(survival) library(geepack) library(data.table) data(Phenobarb) Phenobarb$event <- 1-as.numeric(is.na(Phenobarb$conc)) data <- Phenobarb data <- data[data$event==1,] data$id <- as.numeric(data$Subject) data <- data[data$time<16*24,] data <- data[order(data$id,data$time),] head(data) ``` -------------------------------- ### Summarize Fitted GEE Model Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html This snippet demonstrates how to obtain a summary of the fitted visit intensity model from the `iiwgee` object. This provides coefficients, standard errors, and p-values for the model parameters. ```r summary(iiwgee$phfit) ``` -------------------------------- ### Multiple Outputation with GEE in R Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html This snippet demonstrates multiple outputation using the 'geesmv' package in R. It defines a regression function, truncates inverse-intensity weights, and performs multiple outputations to analyze data with potential collinearity issues. ```R library(geesmv) reg <- function(data){ m <- geem(conc~time + I(time^3), id=id,data=data) est <- cbind(m$beta,summary(m)$se.robust) if(max(table(data$id))>1) est[,2] <- GEE.var.md(conc~time + I(time^3) , id=id,data=data)$cov.beta est <- data.matrix(est) return(est) } set.seed(301031) wt <- i$iiw.weight wt[wt>quantile(i$iiw.weight,0.95)] <- quantile(i$iiw.weight,0.95) m.mogee <- mo(20,reg,data,wt, singleobs=FALSE,id="id",time="time",keep.first=FALSE,var=TRUE) m.mogee ``` -------------------------------- ### Fit Multiple Outputation Liang Model (mo) Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Fits the Liang model using multiple outputation with the calculated weights. The 'mo' function handles the outputation process, and 'singleobs=FALSE' indicates that multiple observations per subject are used. ```R m.moLiang <- mo(20,Liangmo,data,wt, singleobs=FALSE,id="id",time="time",keep.first=FALSE,var=FALSE,Yname="conc", Xnames=c("ApgarInd","ApgarInd.time","ApgarInd.time3"), Wnames=c("Intercept"),maxfu=16*24,baseline=0) ``` -------------------------------- ### Multiple Outputation using GEE Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html This code performs multiple outputation using generalized estimating equations (GEE). It defines a regression function, truncates inverse-intensity weights, and then uses the 'mo' function to generate and analyze multiple datasets. ```R library(geesmv) reg <- function(data){ est <- summary(geeglm(conc~time + I(time^3), id=id,data=data))$coefficients[,1:2] if(max(table(data$id))>1) est[,2] <- GEE.var.md(conc~time + I(time^3) , id=id,data=data)$cov.beta est <- data.matrix(est) return(est) } set.seed(301031) wt <- i$iiw.weight wt[wt>quantile(i$iiw.weight,0.95)] <- quantile(i$iiw.weight,0.95) m.mogee <- mo(20,reg,data,wt, singleobs=FALSE,id="id",time="time",keep.first=FALSE,var=TRUE) m.mogee ``` -------------------------------- ### Multiple Outputation with Liang Model Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html Performs multiple outputation using the 'mo' function with the defined Liangmo function and calculated weights. It specifies covariates, time, and other model parameters. ```R m.moLiang <- mo(20,Liangmo,data,wt, singleobs=FALSE,id="id",time="time",keep.first=FALSE,var=FALSE,Yname="conc", Xnames=c("ApgarInd","ApgarInd.time","ApgarInd.time3"), Wnames=c("Intercept"),maxfu=16*24,baseline=0) ``` -------------------------------- ### Selecting First-Order Fractional Polynomial for Time Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Calculates the adjusted R-squared for various fractional polynomial transformations of time (including log) to find the best single-term fit for concentration. The data should be in a data frame with 'conc' and 'time' columns. ```R rsq1 <- array(dim=8) rsq1[1] <- summary(lm(conc~time,data=data))$adj.r.squared rsq1[2] <- summary(lm(conc~I((time)^0.5),data=data))$adj.r.squared rsq1[3] <- summary(lm(conc~I((time)^2),data=data))$adj.r.squared rsq1[4] <- summary(lm(conc~I((time)^3),data=data))$adj.r.squared rsq1[5] <- summary(lm(conc~log(time),data=data))$adj.r.squared rsq1[6] <- summary(lm(conc~I((time)^(-0.5)),data=data))$adj.r.squared rsq1[7] <- summary(lm(conc~I((time)^(-1)),data=data))$adj.r.squared rsq1[8] <- summary(lm(conc~I((time)^(-2)),data=data))$adj.r.squared which.max(rsq1) ## [1] 5 rsq1[which.max(rsq1)] ## [1] 0.1952896 ``` -------------------------------- ### Fit Unweighted GEE and Plot Trajectories Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Fits an unweighted GEE model and then plots both the unweighted and inverse-intensity weighted GEE model trajectories against the observed data. This allows for visual comparison of the two fitting methods. ```R m <- geeglm(conc ~ time + I(time^3) + log(time) , id=Subject, data=data) time <- (2:200) unweighted <- cbind(rep(1,199),time,time^3,log(time))%*%m$coefficients weighted <- cbind(rep(1,199),time,time^3,log(time))%*%iiwgee$geefit$coefficients plot(data$time,data$conc,xlim=c(0,199),ylim=c(min(unweighted,weighted,data$conc),max(unweighted,weighted,data$conc)),pch=16,xlab="Time",ylab="Serum phenobarbital concentration") lines(time,unweighted,type="l") lines(time,weighted,col=2) legend (0,60,legend=c("Unweighted","Inverse-intensity weighted"),col=1:2,bty="n",lty=1) ``` -------------------------------- ### Weighted Survival Model for Visit Intensity Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Calculates weights for survival analysis to model visit intensity, incorporating covariates and clustering. The 'i$m' output provides the model summary. ```R i <- iiw.weights(Surv(time.lag,time,event)~Wt + I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30)+ cluster(Subject),id="Subject",time="time",event="event",data=data, invariant=c("Subject","Wt"),lagvars=c("time","conc"),maxfu=16*24,lagfirst=c(0,0),first=FALSE) i$m ``` ```text ## Call: ## coxph(formula = Surv(time.lag, time, event) ~ Wt + I(conc.lag > ## 0 & conc.lag <= 20) + I(conc.lag > 20 & conc.lag <= 30) + ## I(conc.lag > 30), data = datacox, cluster = Subject) ## ## coef exp(coef) se(coef) robust se ## Wt -0.13679 0.87215 0.11897 0.11778 ## I(conc.lag > 0 & conc.lag <= 20)TRUE -2.26719 0.10360 0.32831 0.40747 ## I(conc.lag > 20 & conc.lag <= 30)TRUE -2.66595 0.06953 0.33457 0.37910 ## I(conc.lag > 30)TRUE -2.99587 0.04999 0.42698 0.47295 ## z p ## Wt -1.161 0.245 ## I(conc.lag > 0 & conc.lag <= 20)TRUE -5.564 2.63e-08 ## I(conc.lag > 20 & conc.lag <= 30)TRUE -7.032 2.03e-12 ## I(conc.lag > 30)TRUE -6.334 2.38e-10 ## ## Likelihood ratio test=70.86 on 4 df, p=1.496e-14 ## n= 213, number of events= 154 ``` -------------------------------- ### Load MathJax for Mathematical Notation Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html This JavaScript code snippet dynamically loads the MathJax library into the HTML document's head. It's useful for rendering mathematical equations and symbols, especially in scientific or statistical contexts. ```javascript (function () { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; document.getElementsByTagName("head")[0].appendChild(script); })(); ``` -------------------------------- ### Apply Pandoc Div SourceCode Style to Pre SourceCode Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html This JavaScript snippet modifies CSS rules to apply the styling of 'div.sourceCode' to 'pre.sourceCode', enhancing code block presentation. ```javascript (function() { var sheets = document.styleSheets; for (var i = 0; i < sheets.length; i++) { if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; try { var rules = sheets[i].cssRules; } catch (e) { continue; } var j = 0; while (j < rules.length) { var rule = rules[j]; if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { j++; continue; } var style = rule.style.cssText; if (rule.style.color === '' && rule.style.backgroundColor === '') { j++; continue; } sheets[i].deleteRule(j); sheets[i].insertRule('pre.sourceCode{' + style + '}', j); } } })(); ``` -------------------------------- ### Fit Inverse-Intensity Weighted GEE model Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html This snippet fits an inverse-intensity weighted GEE model using the `iiwgee` function. It specifies the formula for the GEE and the formula for the inverse intensity weights, along with other necessary parameters like id, time, and event. ```R iiwgee <- iiwgee(conc ~ time + I(time^3) + I(log(time)),Surv(time.lag,time,event)~I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30) +cluster(id), formulanull=NULL,id="id",time="time",event="event",data=data, invariant=c("id","Wt"),lagvars=c("time","conc"),maxfu=16*24,lagfirst=c(0,0),first=FALSE) ``` -------------------------------- ### Prepare Data for Liang Model Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Adds necessary columns to the data frame for the Liang model analysis, including an intercept, time cubed, log time, and interaction terms involving Apgar score and time. ```R data$Intercept <- 1 data$time3 <- (data$time)^3 data$logtime <- log(data$time) data$ApgarInd.time <- as.numeric(data$ApgarInd)*data$time/24 data$ApgarInd.time3 <- as.numeric(data$ApgarInd)*((data$time/24)^3) ``` -------------------------------- ### Summarize Inverse-Intensity Weighted GEE Fit Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Provides a summary of the inverse-intensity weighted GEE fit obtained from the iiwgee function. This includes coefficients, standard errors, Wald statistics, and p-values for the model terms. ```R summary(iiwgee$geefit) ``` -------------------------------- ### Selecting Second-Order Fractional Polynomial for Time Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Calculates the adjusted R-squared for second-order fractional polynomial models, combining a log-transformed time with other powers and interaction terms. This helps to improve the fit beyond a single-term model. Ensure 'conc' and 'time' are available in the 'data' data frame. ```R rsq2 <- array(dim=8) rsq2[1] <- summary(lm(conc~log(time)+ time,data=data))$adj.r.squared rsq2[2] <- summary(lm(conc~log(time)+ I((time)^0.5),data=data))$adj.r.squared rsq2[3] <- summary(lm(conc~log(time) + I((time)^2),data=data))$adj.r.squared rsq2[4] <- summary(lm(conc~log(time)+ I((time)^3),data=data))$adj.r.squared rsq2[5] <- summary(lm(conc~log(time)+ time:log(time),data=data))$adj.r.squared rsq2[6] <- summary(lm(conc~log(time) + I((time)^(-0.5)))*log(1+time),data=data))$adj.r.squared rsq2[7] <- summary(lm(conc~log(time) + I((time)^(-1)),data=data))$adj.r.squared rsq2[8] <- summary(lm(conc~log(time)+ I((time)^(-2)),data=data))$adj.r.squared which.max(rsq2) ## [1] 4 rsq2[which.max(rsq2)] ## [1] 0.3468345 ``` -------------------------------- ### Define Liang Model Function Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Defines a wrapper function for the Liang model to include printing and returning the model object. It utilizes helper functions Xfn and Wfn for time-invariant and time-dependent covariates respectively. ```R Liangmo <- function(data,Yname,Xnames,Wnames,maxfu,baseline){ x <- Liang(data=data,Yname=Yname,Xnames=Xnames,Wnames=Wnames,id="id",time="time", maxfu=maxfu,baseline=baseline,Xfn=Xfn,Wfn=Wfn); print(x); return(x) } ``` -------------------------------- ### Model Estimates Output Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Displays the estimated coefficients from the multiple outputation Liang model. The first three are for Apgar score and its time interactions, the fourth is theta, and the last is the frailty variance. ```R ## [1] -20.15766 5.25885 -0.02689 NA 0.00000 ``` -------------------------------- ### Summarize Events Per Subject Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Calculates and displays summary statistics for the number of events per subject. Useful for understanding the distribution of events across subjects. ```R summary(tapply(data$event,data$Subject,sum)) ``` -------------------------------- ### Fit various time transformations and select best model Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Calculates the adjusted R-squared for linear models with different transformations of time (linear, square root, square, cube, log, inverse square root, inverse, inverse square). Selects the transformation that yields the highest adjusted R-squared. ```R rsq1 <- array(dim=8) rsq1[1] <- summary(lm(conc~time,data=data,weight=i$iiw.weight))$adj.r.squared rsq1[2] <- summary(lm(conc~I((time)^0.5),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[3] <- summary(lm(conc~I((time)^2),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[4] <- summary(lm(conc~I((time)^3),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[5] <- summary(lm(conc~log(time),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[6] <- summary(lm(conc~I((time)^(-0.5)),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[7] <- summary(lm(conc~I((time)^(-1)),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[8] <- summary(lm(conc~I((time)^(-2)),data=data,weight=i$iiw.weight))$adj.r.squared which.max(rsq1) ## [1] 4 rsq1[which.max(rsq1)] ## [1] 0.163227 ``` -------------------------------- ### Define Time-Invariant Covariate Function (Xfn) Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Specifies how to extract time-invariant covariates for the Liang model. This function assumes the covariate is constant for each subject and uses the first available value. ```R Xfn <- function(id,time){ # Group is time invariant so just use the first value for each subject return(as.numeric(data$ApgarInd[data$id==id][1])) } ``` -------------------------------- ### Extract Model Estimates Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Retrieves the estimated coefficients from the fitted multiple outputation Liang model. The output includes coefficients for Apgar score and its time interactions, theta, and frailty variance. ```R m.moLiang$est ``` -------------------------------- ### Liang Model Function Definition Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html Defines a wrapper function for the Liang model, printing the results and returning the model object. It utilizes helper functions Xfn and Wfn for covariate definitions. ```R Liangmo <- function(data,Yname,Xnames,Wnames,maxfu,baseline){ x <- Liang(data=data,Yname=Yname,Xnames=Xnames,Wnames=Wnames,id="id",time="time", maxfu=maxfu,baseline=baseline,Xfn=Xfn,Wfn=Wfn); print(x); return(x) } ``` -------------------------------- ### Calculate Inverse Intensity Weights (iiw.weights) Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Calculates weights for multiple outputation using the iiw.weights function. This is crucial when the visit process depends on covariates, especially when frailty is involved and the Cox model is non-collapsible. ```R set.seed(301031) ifrailty <- iiw.weights(Surv(time.lag,time,event)~I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30) +frailty(id),id="id",time="time",event="event",data=data, invariant=c("id"),lagvars=c("time","conc"),maxfu=16*24, lagfirst=c(0,0), first=FALSE) wt <- ifrailty$iiw.weight ``` -------------------------------- ### Plotting Concentration Over Time Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Generates a scatter plot of serum concentration against time, with specified axis limits and labels. This is used to visualize the relationship before modeling. ```R plot(data$time,data$conc,xlim=c(0,200),pch=16, xlab="Time in hours", ylab="Concentration") ``` -------------------------------- ### Fit Inverse-Intensity Weighted GEEs Source: https://github.com/epullenayegum/irreglong/blob/master/README.md Use the `iiwgee` function to fit inverse-intensity weighted GEEs, accounting for irregularity in longitudinal data. The summary of the fitted GEE model is then displayed. ```r iiwgee <- iiwgee(conc ~ time + I(time^3) + log(time),Surv(time.lag,time,event)~I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30) +cluster(id), formulanull=NULL,id="id",time="time",event="event",data=data, invariant=c("id","Wt"),lagvars=c("time","conc"),maxfu=16*24,lagfirst=c(0,0),first=FALSE) summary(iiwgee$geefit) ``` -------------------------------- ### Fit Inverse-Intensity Weighted GEE Model Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Fits an inverse-intensity weighted GEE model using the iiwgee function. The formula includes time, time cubed, and log(time). The function also specifies the formula for inverse intensity weights, id, time, event, data, and lag variables. ```R iiwgee <- iiwgee(conc ~ time + I(time^3) + log(time),Surv(time.lag,time,event)~I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30) +cluster(id), formulanull=NULL,id="id",time="time",event="event",data=data, invariant=c("id","Wt"),lagvars=c("time","conc"),maxfu=16*24,lagfirst=c(0,0),first=FALSE) ``` -------------------------------- ### Fit models with cubic time and other transformations Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Calculates the adjusted R-squared for models that include time cubed along with other transformations (linear, square root, square, log, inverse square root, inverse, inverse square). Selects the combination yielding the highest adjusted R-squared. ```R rsq1 <- array(dim=8) rsq1[1] <- summary(lm(conc~I((time)^3) +time,data=data,weight=i$iiw.weight))$adj.r.squared rsq1[2] <- summary(lm(conc~I((time)^3) +I((time)^0.5),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[3] <- summary(lm(conc~I((time)^3) +I((time)^2),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[4] <- summary(lm(conc~I((time)^3) + log(time):I(time^3),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[5] <- summary(lm(conc~I((time)^3) +log(time),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[6] <- summary(lm(conc~I((time)^3) +I((time)^(-0.5)),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[7] <- summary(lm(conc~I((time)^3) +I((time)^(-1)),data=data,weight=i$iiw.weight))$adj.r.squared rsq1[8] <- summary(lm(conc~I((time)^3) +I((time)^(-2)),data=data,weight=i$iiw.weight))$adj.r.squared which.max(rsq1) ## [1] 1 rsq1[which.max(rsq1)] ## [1] 0.3353907 ``` -------------------------------- ### Create Abacus Plot for Irregularity Source: https://github.com/epullenayegum/irreglong/blob/master/README.md Generate an abacus plot to visually assess the extent of irregularity in the data. This plot helps determine how closely the data resembles repeated measures data. ```r abacus.plot(n=59,time="time",id="Subject",data=data,tmin=0,tmax=16*24, xlab.abacus="Time in hours",pch=16,col.abacus=gray(0.8)) ``` -------------------------------- ### Cox Proportional Hazards Model Output Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html This output shows the results of a coxph model, including coefficients, standard errors, and significance levels. It is useful for understanding the impact of covariates on survival time. ```R ## Call: ## coxph(formula = Surv(time.lag, time, event) ~ I(conc.lag > 0 & ## conc.lag <= 20) + I(conc.lag > 20 & conc.lag <= 30) + I(conc.lag > ## 30), data = datacox, cluster = id) ## ## n= 213, number of events= 154 ## ## coef exp(coef) se(coef) robust se ## I(conc.lag > 0 & conc.lag <= 20)TRUE -2.2745 0.1028 0.3315 0.4050 ## I(conc.lag > 20 & conc.lag <= 30)TRUE -2.6733 0.0690 0.3368 0.3740 ## I(conc.lag > 30)TRUE -2.9998 0.0498 0.4279 0.4676 ## z Pr(>|z|) ## I(conc.lag > 0 & conc.lag <= 20)TRUE -5.62 1.9e-08 *** ## I(conc.lag > 20 & conc.lag <= 30)TRUE -7.15 8.8e-13 *** ## I(conc.lag > 30)TRUE -6.42 1.4e-10 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## exp(coef) exp(-coef) lower .95 upper .95 ## I(conc.lag > 0 & conc.lag <= 20)TRUE 0.1028 9.72 0.0465 0.227 ## I(conc.lag > 20 & conc.lag <= 30)TRUE 0.0690 14.49 0.0332 0.144 ## I(conc.lag > 30)TRUE 0.0498 20.08 0.0199 0.125 ## ## Concordance= 0.624 (se = 0.014 ) ## Likelihood ratio test= 69.5 on 3 df, p=6e-15 ## Wald test = 54.4 on 3 df, p=9e-12 ## Score (logrank) test = 69.1 on 3 df, p=7e-15, Robust = 56.1 p=4e-12 ## ## (Note: the likelihood ratio and score tests assume independence of ## observations within a cluster, the Wald and robust score tests do not). ``` -------------------------------- ### Weighted Survival Model Without Wt Covariate Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Calculates weights for survival analysis, excluding the 'Wt' covariate to assess its impact. The 'i$m' output provides the model summary. ```R i <- iiw.weights(Surv(time.lag,time,event)~I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30) + cluster(Subject),id="Subject",time="time",event="event",data=data, invariant=c("Subject","Wt"),lagvars=c("time","conc"),maxfu=16*24,lagfirst=c(0,0),first=FALSE) i$m ``` ```text ## Call: ## coxph(formula = Surv(time.lag, time, event) ~ I(conc.lag > 0 & ## conc.lag <= 20) + I(conc.lag > 20 & conc.lag <= 30) + I(conc.lag > ## 30), data = datacox, cluster = Subject) ## ## coef exp(coef) se(coef) robust se ## I(conc.lag > 0 & conc.lag <= 20)TRUE -2.27453 0.10284 0.33148 0.40497 ## I(conc.lag > 20 & conc.lag <= 30)TRUE -2.67331 0.06902 0.33681 0.37398 ## I(conc.lag > 30)TRUE -2.99982 0.04980 0.42794 0.46762 ## z p ## I(conc.lag > 0 & conc.lag <= 20)TRUE -5.617 1.95e-08 ## I(conc.lag > 20 & conc.lag <= 30)TRUE -7.148 8.79e-13 ## I(conc.lag > 30)TRUE -6.415 1.41e-10 ## ## Likelihood ratio test=69.47 on 3 df, p=5.542e-15 ## n= 213, number of events= 154 ``` -------------------------------- ### Fit Inverse Intensity Weighted GEE with Lagged Covariates Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html This snippet calculates inverse-intensity weights and fits a weighted GEE model. It requires specifying time periods at risk by lagging time and potentially other covariates. The `iiw.weights` function handles the creation of lagged variables internally. ```R data$Apgar <- as.numeric(data$Apgar) i <- iiw.weights(Surv(time.lag,time,event)~Wt + Apgar + I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30)+ cluster(Subject),id="Subject",time="time",event="event",data=data, invariant=c("Subject","Wt","Apgar"),lagvars=c("time","conc"),maxfu=16*24, lagfirst=c(0,0),first=FALSE) i$m ``` -------------------------------- ### Fit Simplified GEE Model with Indicator Variable Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html This snippet fits a simplified GEE model where the Apgar score is included as an indicator variable. It demonstrates how to modify the model formula to include an indicator and potentially remove non-significant variables from the previous model. ```R i <- iiw.weights(Surv(time.lag,time,event)~Wt + ApgarInd + I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30)+ cluster(Subject),id="Subject",time="time",event="event",data=data, invariant=c("Subject","Wt","ApgarInd"),lagvars=c("time","conc"), maxfu=16*24,lagfirst=c(0,0),first=FALSE) i$m ``` -------------------------------- ### Calculate Extent of Irregularity Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html Calculates the extent of irregularity in longitudinal data. Use this to understand data sparsity and inform modeling choices. Requires specifying time and ID variables, and optionally cutpoints, follow-up duration, plotting parameters, and a survival formula. ```r counts <- extent.of.irregularity(data,time="time",id="id", scheduledtimes=NULL, cutpoints=NULL,ncutpts=50, maxfu=16*24, plot=TRUE,legendx=30,legendy=0.8, formula=Surv(time.lag,time,event)~1,tau=16*24) ``` -------------------------------- ### Calculate Weighted Survival with Covariates Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html Calculates weighted survival using the `iiw.weights` function, including time-varying covariates and clustering. This is useful for modeling time to event when observations are irregular. ```R i <- iiw.weights(Surv(time.lag,time,event)~Wt + I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30)+ cluster(Subject),id="Subject",time="time",event="event",data=data, invariant=c("Subject","Wt"),lagvars=c("time","conc"),maxfu=16*24,lagfirst=c(0,0),first=FALSE) i$m ``` ```R i <- iiw.weights(Surv(time.lag,time,event)~I(conc.lag>0 & conc.lag<=20) + I(conc.lag>20 & conc.lag<=30) + I(conc.lag>30) + cluster(Subject),id="Subject",time="time",event="event",data=data, invariant=c("Subject","Wt"),lagvars=c("time","conc"),maxfu=16*24,lagfirst=c(0,0),first=FALSE) i$m ``` -------------------------------- ### Calculate Extent of Irregularity Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/Irreglong-vignette.html Use extent.of.irregularity to calculate and plot the extent of irregularity in longitudinal data. Ensure 'time', 'id', 'formula', and 'tau' are correctly specified. ```R counts <- extent.of.irregularity(data,time="time",id="id", scheduledtimes=NULL, cutpoints=NULL,ncutpts=50, maxfu=16*24, plot=TRUE,legendx=30,legendy=0.8, formula=Surv(time.lag,time,event)~1,tau=16*24) ``` -------------------------------- ### Remove Header Attributes in Pandoc Source: https://github.com/epullenayegum/irreglong/blob/master/vignettes/pandoc4ae8d80e2f.html This JavaScript code removes attributes from the first child element of section divs, ensuring compatibility with older Pandoc versions. ```javascript document.addEventListener('DOMContentLoaded', function(e) { var hs = document.querySelectorAll("div.section\[class*='level'\] > :first-child"); var i, h, a; for (i = 0; i < hs.length; i++) { h = hs[i]; if (!/^h[1-6]$/i.test(h.tagName)) continue; a = h.attributes; while (a.length > 0) h.removeAttribute(a[0].name); } }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.