Does season package support splines? - r

I was running a casecross function in season package with this code
library(season)
library(splines)
data(CVDdaily)
CVDdaily<-subset(CVDdaily,date<=as.Date('1987-12-31'))
# Effect of ozone on CVD death
model1<- casecross(cvd ~ o3mean+tmpd+Mon+Tue+Wed+Thu+Fri+Sat, data=CVDdaily)
But when I use the natural splines I get an error message "Error in ns(tmpd, df = 6) : object 'tmpd' not found"
model2<-casecross(cvd ~ o3mean + ns(tmpd, df=6) +Mon+Tue+Wed+Thu+Fri+Sat, data=CVDdaily)
Does it mean that the package has no support for splines? If yes, I would appreciate any ideas on how to adjust the nonlinear effect of temperature using a season package?

You can fit splines in season using the dlnm package. (I've used 4 degrees of freedom rather than 6).
library(season)
library(dlnm)
data(CVDdaily)
CVDdaily = subset(CVDdaily,date<=as.Date('1987-12-31'))
t.spline = crossbasis(CVDdaily$tmpd,lag=0,argvar=list(fun="ns",df=4))
model2 = casecross(cvd ~ o3mean + t.spline +Mon+Tue+Wed+Thu+Fri+Sat, data=CVDdaily)
summary(model2)
To get a nice plot of the estimated spline you need to extract the coefficients and the variance-covariance matrix.
coef = coefficients(model2$c.model)[2:5]
var.cov = model2$c.model$var[2:5,2:5]
pred.t = crosspred(basis=t.spline, at=45:86, vcov=var.cov, coef=coef, model.link='identity')
plot(pred.t,"overall")

Related

Probing interactions in nlme using the "interactions" package in R

I am running a linear mixed effects models using the "nlme" package looking at stress and lifestyle as predictors of change in cognition over 4 years in a longitudinal dataset. All variables in the model are continuous variables.
I am able to create the model and get the summary statistics using this code:
mod1 <- lme(MS ~ age + sex + edu + GDST1*Time + HLI*Time + GDST1*HLI*Time, random= ~ 1|ID, data=NuAge_long, na.action=na.omit)
summary(mod1)
I am trying to use the "interactions" package to probe the 3-way interaction:
sim_slopes(model = mod1, pred = Time, modx = GDST1, mod2 = HLI, data = NuAge_long)
but am receiving this error:
Error in if (tcol == "df") tcol <- "t val." : argument is of length zero
I am also trying to plot the interaction using the same "interactions" package:
interact_plot(model = mod1, pred = Time, modx = GDST1, mod2 = HLI, data = NuAge_long)
and am receiving this error:
Error in UseMethod("family") : no applicable method for 'family' applied to an object of class "lme"
I can't seem to find what these errors mean and why I'm getting them. Any help would be appreciated!
From ?interactions::sim_slopes:
The function is tested with ‘lm’, ‘glm’,
‘svyglm’, ‘merMod’, ‘rq’, ‘brmsfit’, ‘stanreg’ models. Models
from other classes may work as well but are not officially
supported. The model should include the interaction of
interest.
Note this does not include lme models. On the other hand, merMod models are those generated by lme4::[g]lmer(), and as far as I can tell you should be able to fit this model equally well with lmer():
library(lme4)
mod1 <- lmer(MS ~ age + sex + edu + GDST1*Time + HLI*Time + GDST1*HLI*Time
+ (1|ID), data=NuAge_long)
(things will get harder if you want to specify correlation structures, e.g. correlation = corAR1(), which works for lme() but not lmer() ...)

Finding area unde the curve in a Gompertz distribution in R

I am trying to fit a gompertz model to survival data. I am using the package 'flexsurv', and after setting up the data I use the following especification:
Gompertz.fit <- flexsurvreg(surv.age ~ Region + Sex + Income,
data = SA_Data, dist = "gompertz")
As I want to estimate the area under the curve but couldn't find a command, I thought about estimating the AUC "by hand", for which I need the gamma parameter. However, I can't seem to find it. When I try the survreg to estimate the parameters I get the following answer
> result.survreg.0 <- survreg(Surv(Age_At_DC, status)~1, data = SA_Data, dist = "gompertz") Error in match.arg(dist,
names(survreg.distributions)) : 'arg' should be one of “extreme”,
“logistic”, “gaussian”, “weibull”, “exponential”, “rayleigh”,
“loggaussian”, “lognormal”, “loglogistic”, “t”
Has anyone else estimated the AUC with a gompertz distribution in R?

Error running betar distriubtion in GAM mgcv

I'm trying to run a gam using the mgcv package with a response variable which is proportional data. The data is overdispered so initially I used a quasibinomial distribution. However because I'm using model selection that's not particularly useful as it does not produce AIC scores.
Instead I'm trying to use betar distribution, as I've read that it could be appropriate.
mRI_br <- bam(ri ~ SE_score + s(deg, k=7) + s(gs, k=7) + TL + species + sex + season + year + s(code, bs = 're') + s(station, bs = 're'), family=betar(), data=node_dat, na.action = "na.fail")
However I'm getting this warnings when I run the model.
Warning messages:
1: In estimate.theta(theta, family, y, mu, scale = scale1, ... :
step failure in theta estimation
And when I try and check the model summary I get this error.
> summary(mRI_br)
Error in chol.default(diag(p) + crossprod(S2 %*% t(R2))) :
the leading minor of order 62 is not positive definite
I would like to know:
What is causing these errors and warnings, and how can they be solved?
If not are there any other distributions that can be used with proportion data which enable me to subsequently use model selection techniques (such as the dredge function from the MuMIn package.
A copy of the dataset can be found here

Goodness of Fit statistic Tobit model

I have estimated a Tobit model using the censReg package, along with the censReg function. Alternatively, the same Tobit model is estimated using the tobit function in the AER package.
Now, I really like to have some goodness of fit statistic, such as the Pseudo-R2. However, whenever I try to estimate this, the output returns as NA. For example:
Tobit <- censReg(Listing$occupancy_rate ~ ., left = -Inf, right = 1, data = Listing)
PseudoR2(Tobit, which = "McFadden")
[1] NA
So far, I have only seen reported Pseudo-R2's when people use Stata. Does anyone know how to estimate it in R?
Alternatively, Tobit estimates the (log)Sigma, which is basically the standard deviation of the residuals. Could I use this to calculate the R2?
All help is really appreciated.
You can use DescTools package to calculate PseudoR2. You have not provided any sample data. So, it is hard for me to run your model. I am using a default dataset like
library(DescTools)
r.glm <- glm(Survived ~ ., data=Untable(Titanic), family=binomial)
PseudoR2(r.glm, c("McFadden"))
For your model, you can use something like
library(AER)
data("Affairs", package = "AER")
fm.tobit <- tobit(affairs ~ age + yearsmarried + religiousness + occupation + rating,
data = Affairs)
#Create a function for pseudoR2 calculation
pseudoR2 <- function(obj) 1 - as.vector(logLik(obj)/logLik(update(obj, . ~ 1)))
pseudoR2(fm.tobit)
#>[1] 0.05258401
Or using censReg as you have used
library(censReg)
data("Affairs", package = "AER")
estResult <- censReg(affairs ~ age + yearsmarried + religiousness +
occupation + rating, data = Affairs)
summary(estResult)
pseudoR2(estResult)
#>[1] 0.05258401
You can find the details about pseudoR2 in the following link
R squared in logistic regression

Running Cox.ph model with GAMM mixed models in R

I am new in using GAM and splines. I am running a survival model in which I want to model the Time to event with the age of the subjects controlling by two variables. Here is the example using a conventional survival model with coxph:
library(survival)
fit_cox<-coxph(Surv(time, event)~ age+ var1 + var2, data=mydata)
I suspect that the relationship between var1 and var2 with the outcome is not linear and also I am thinking that I can include random effects in my model (moving to mixed effect models gamm).
I have tried this syntax:
library(mgcv)
fit_surv<-Surv(time, event)
fit_gam<-gam(fit_surv ~ age + s(var1) + s(var2), data = mydata, family = cox.ph())
And to include the random effects:
library(gamm4)
fit_gamm <- gamm4(fit_surv ~ age + s(var1) + s(var2), random = ~(1 | ID), data = mydata, family = cox.ph)
My problems are:
1. In fit_gam I do not know how to make a summary of this model and to see the coefficients table and plot the model. This error came to me:
summary(fit_gam)
"Error in Ops.Surv(w, object$y) : Invalid operation on a survival time"
In fit_gamm I could not run the model because some error in syntaxis is made or maybe I could not include a surv? The error is:
"Error in ncol(x) : object 'x' not found"
Thank you in advance!
As mentioned in the comments, simple gaussian frailties (gaussian random intercept) can be specified directly within the mgcv::gam call, e.g. by adding ... + s(ID, bs = "re") + ... to your formula (note that ID has to be a factor variable).
Alternatively, you can transform the data to the so called Piece-wise Exponential Data (PED) format and fit the model using any GA(M)M software, which are then called Piece-wise exponential Additive Mixed Models (PAMM). Here is an example:
library(coxme)
library(mgcv)
library(pammtools)
lung <- lung %>% mutate(inst = as.factor(inst)) %>% na.omit()
## cox model with gaussian frailty
cme <- coxme(Surv(time, status) ~ ph.ecog + (1|inst), data=lung)
## pamm with gaussian frailty
ped <- lung %>% as_ped(Surv(time, status)~., id="id")
pam <- gam(ped_status ~ s(tend) + ph.ecog + s(inst, bs = "re"),
data = ped, family = poisson(), offset = offset)
## visualize random effect:
gg_re(pam)
# compare coxme and pamm estimates:
re <- tidy_re(pam)
plot(cme$frail$inst, re$fit, las=1, xlab="Frailty (cox)", ylab="Frailty (PAM)")
abline(0, 1)
## with gamm4
library(gamm4)
#> Loading required package: Matrix
#> Loading required package: lme4
#>
#> Attaching package: 'lme4'
#> The following object is masked from 'package:nlme':
#>
#> lmList
#> This is gamm4 0.2-5
pam2 <- gamm4(ped_status ~ s(tend) + ph.ecog, random = ~(1|inst),
family = poisson(), offset = ped$offset, data = ped)
lattice::qqmath(ranef(pam2$mer)$inst[, 1])
Created on 2018-12-08 by the reprex package (v0.2.1)

Resources