EXtract the AIC value from a list of survival models - r

I have the following list of model
list_model <- list(fit1, fit2, fit.coxph1, fit.coxph2)
fit1 and fit2 have been realized as follows:
surv_object <- Surv(time = data$`Last observation (days)`, event = data$Death)
surv_object
fit1 <- survfit(surv_object ~ Treatment, data = data)
summary(fit1)
fit2 <- survfit(surv_object ~ Treatment + data$`Age (years)_cat`, data = data)
summary(fit2)
While the last ones are Cox models:
fit.coxph1 <- coxph(surv_object ~ Treatment,
data = data)
fit.coxph1
ggforest(fit.coxph1, data = data)
fit.coxph2 <- coxph(surv_object ~ Treatment + `Age (years)_cat`,
data = data)
fit.coxph2
Does anyone know how to extract iteratively the AIC values from them?
thanks

Related

Only calling a Binomial model

I am under the assumption that there are issues just calling a binomial model in R without specifying the family type (logit/probit) right?
model <- glm(y ~ x, family = binomial, data = data)
vs
model_2 <- glm(y ~ x, binomial(link = "logit"), data = data)

How to use tensor function of gam model in train function of caret package in r?

I am using gam method with both spline and tensor interaction functions(s and ti) inside the train function (for test and train).
I know for spline functions in gam we can use method = "gam" in train function. for example:
fit <- gam(Y ~ s(x1) + s(x2) + s(x3) , data=df)
Prediction_gam <- as.numeric(predict(fit , data=df , type = "response"))
can be changed to follow for test and train in caret package:
fit_train <- train(Y ~ x1 + x2 + x3 , data = train_df, method = "gam", trControl = train.control)
but I don't know how to add tensor interaction function of gam in train function for example:
fit <- gam(Y ~ s(x1) + s(x2) + s(x3) + ti(x1,x2) , data=df)
any suggestion would be appreciated.
the full codes are as follow:
library(caret)
df <- data.frame(Y=rnorm(100), x1=rnorm(100),x2=rnorm(100), x3=rnorm(100))
df <- as.data.frame(do.call(cbind, df))
set.seed(1)
training.samples <- df$x1%>%createDataPartition(p = 0.8, list = FALSE)
train_df <- df[training.samples, ]
test_df <- df[-training.samples, ]
train.control <- trainControl(method = "repeatedcv", number = 10, repeats = 2)
fit_train <- train(Y ~ x1 + x2 + x3 , data = train_df, method = "gam", trControl = train.control)
Prediction_train <- as.numeric(predict(fit_train , data=train_df , type = "raw"))
Prediction_test <- as.numeric(predict(fit_train , newdata =test_df , type = "raw"))

LavaanPlot Floating Circle

I simulated the following data:
library(lavaan)
library(lavaanPlot)
set.seed(2002)
#simulate predictor variables
pred1<- c(1:60)
pred2<- rnorm(60, mean=100, sd=10)
pred3 <- .05 + .05*pred1 + rnorm(length(pred1),1,.5)
#simulate response variables
resp <- 350 -2*pred1 -50*pred3 + rnorm(length(pred1),50,50)
#create df
df <- cbind(resp, pred1, pred3, pred2)
Developed the following model:
#sem model
model <-
'pred2 ~ pred1
resp ~ pred1
resp~ pred3
pred3 ~ pred1'
Fit the model:
# fit model
fit <- sem(model, data = df)
summary(fit,rsq = T, fit.measures = TRUE, standardized = TRUE)
Using the lavaanPlot function I get a floating bubble in the right corner. I would like to know what it means, why it appears and how to remove it from the output diagram.
lavaanPlot(name = "MODEL1", fit, labels = df, coefs = TRUE)

Run HLM mediation in R

I try to run HLM mediation with the "mediation" package:
med.fit <- glmer(M ~ treat + control + (1|subject_id) ,family = binomial(link = "logit"), data = R1_data)
out.fit <- glmer(Y ~ M+ treat + control+ (1 + M|subject_id),family = binomial(link = "logit"), data = R1_data)
med.out <- mediate(med.fit, out.fit, treat = "treat", mediator = "M", sims = 1000)
I got this error message:
Error in [.data.frame(y.data, int.term.name[p]) : undefined columns selected
How to solve this problem?
Here is the original data and code:
names(R1_data)
[1] "subject_id"
[3] "Presented_is_solvable"
[5] "JOS"
[17] "Answer_JOS"
[23] "Matrix_Z_score"
library(mediation)
library(lme4)
med.fit <- glmer(JOS ~ Matrix_Z_score + Presented_is_solvable + (1|subject_id) ,family = binomial(link = "logit"), data = R1_data)
out.fit <- glmer(Answer_JOS ~ JOS + Matrix_Z_score +Presented_is_solvable + (1 + JOS|subject_id),family = binomial(link = "logit"), data = R1_data)
med.out <- mediate(med.fit, out.fit, treat = "Matrix_Z_score", mediator = "JOS", sims = 1000)
Figured out that this happens when treatment or mediator data is classified as factor data in R. The mediate function can't properly locate the names of those variables from the fitted models as in the models, they are displayed as "variablename"+factor level.
The solution is to make sure those variables are classified as integers. You can take a look at the variable classifications in the student data set within the mediation package.

R Predict using multiple models

I am new to R and trying to predict outcomes on a dataset using 4 different GLM's. I have tried running as one large model and while I do get results the model doesn't converge properly and I end up with N/A's. I therefore have four models:
model_team <- glm(mydata$OUT ~ TEAM + OPPONENT, family = "binomial",data = mydata )
model_conf <- glm(mydata$OUT ~ TCONF + OCONF, family = "binomial",data = mydata)
model_tstats <- glm(mydata$OUT ~ TPace + TORtg + TFTr + T3PAr + TTS. + TTRB. + TAST. + TSTL. + TBLK. + TeFG. + TTOV. + TORB. + TFT.FGA, family = "binomial",data = mydata)
model_ostats<- glm(mydata$OUT ~ OPace + OORtg + OFTr + O3PAr + OTS. + OTRB. + OAST. + OSTL. + OBLK. + OeFG. + OTOV. + OORB. + OFT.FGA, family = "binomial",data = mydata)
I then want to predict the outcomes using a different data set using the four models
predict(model_team, model_conf, model_tstats, model_ostats, fix, level = 0.95, type = "probs")
Is there a way to use all four models with joining them into one large set?
I don't really understand why you are trying to do what you are doing. I also don't have any example data that is a representation of the data you are working with. However, below is an example of how you could combine multiple GLMs into one using the resulting coefficients. Note that this will not work well if you have multicollinearity between the variables in your dataset.
# I used the iris dataset for my example
head(iris)
# Run several models
model1 <- glm(data = iris, Sepal.Length ~ Sepal.Width)
model2 <- glm(data = iris, Sepal.Length ~ Petal.Length)
model3 <- glm(data = iris, Sepal.Length ~ Petal.Width)
# Get combined intercept
intercept <- mean(
coef(model1)['(Intercept)'],
coef(model2)['(Intercept)'],
coef(model3)['(Intercept)'])
# Extract coefficients
coefs <- as.matrix(
c(coef(model1)[2],
coef(model2)[2],
coef(model3)[2])
# Get the feature values for the predictions
ds <- as.matrix(iris[,c('Sepal.Width', 'Petal.Length', 'Petal.Width')])
# Linear algebra: Matrix-multiply values with coefficients
prediction <- ds %*% coefs + intercept
# Let's look at the results
plot(iris$Petal.Length, prediction)

Resources