Obtain the CDF of a Vine Copula (trivariate) in R - r

I'm aware that the "VineCopula" package has a RVinePDF function and a BiCopCDF function, however there is no RVineCDF function. I need to select and fit R-Vine Copulas to a subset of three variables with 40 observations in a dataset with 300+ levels of a grouping variable (think of fitting a copula to each municipality in Texas based on precipitation, temperature and solar radiation data).
Is there any alternative using the RVineStructureSelect and RVineCopSelect pipeline?

There are many other vine copula packages, but not sure if they will be good for your question, for example, vinereg (for fitting regression D-vine copula), vinecopulib (similar to the VineCopula but take into account the marginals).

Related

MCAR (Multivariate Conditional Autoregressive) model in R

I am performing a spatial analysis of student grades according to their city of origin using R. I have several covariates such as poverty, education and socio-cultural indices. So far I have fitted univariate models such as: linear regression, weighted linear regression and CAR (conditional autoregressive).
Now, I am reading "Hierarchical Modeling and Analysis for Spatial Data" from Banerjee, Carlin and Gelfand. I am interested in applying multivariate models, in particular a MCAR (Multivariate Conditional Autoregressive) model.
However, I have not found any code in R (or Python) that has it implemented. The most possible has been the "spatialreg" library that includes univariate CAR and SAR models.
Is there any library that you know of that includes them? Thanks in advance
I have found "CARBayes" package. This works perfectly for fitting MCAR model.

Extract sample variance from svykm (survey package by Lumley) for complex survey analysis

In order to compare two survival curves at a fixed point in time and perform basically a two sample test, I need to extract the sample variance of the estimate at a given point in time.
For an object created with the svykm function from Thomas Lumley's survey package in R, this should be accessible in the varlog list. Do the entries in this list constitute the transformed variances on the log scale or the untransformed variances?
I have read the documentation provided for the survey package, but did not fully come to a conclusion. I note that confidence intervals are computed on the log(survival) scale, following the default in survival package and their bounds are given as exp(log(x$surv)+1.96*sqrt(x$varlog)) and exp(log(x$surv)-1.96*sqrt(x$varlog)) in the R package documentation.
They are variances on the log scale.

Create Bi-variate probability density to sample from in R

I have some data that has two variables: spend and outcomes and they are given at a weekly frequency.
I would like to model the relationship between the two at a yearly level, but do not have enough years worth of data to build a model. I do have about 3 years worth of weekly data, however, and would like to simulate several more weeks of data points (spend and outcomes) based on a bi-variate probability density between spend and outcomes which I could then use to roll up to a yearly frequency.
Is there a package in R that can take take two variables and find an estimate for the density function which I could then use to simulate many more data points?
Thanks so much!
The simulate_kde function in the package simukde will internally make a kernel density estimation and create samples from it.
Alternatively, the MASS package has the kde2d function to obtain a bivariate kernel density.
You could then sample from that, as for instance described in this post.

Does the R survey package have a function like prop.test for comparing two population proportions?

I am working with a database that involves weighted statistics and complex survey design, namely the National/Nationwide Inpatient Sample, so I am using R's 'survey' package for tasks like summary statistics, regression, etc.
One test I was interesting in running was comparing the population of a certain group in 2 different populations (i.e. is the difference between the proportion of A in Population B and proportion of A in Population C statistically significant). I have already used scvyciprop to plot the confidence intervals for these proportions, and have seen that these two proportions are significantly different. However, I was wondering if there was a function like prop.test, whether or not it's in the 'survey' package, that can run this test for data in a complex survey (ex. takes a survey design object as an argument) and actually quantify the p-value / t-statistic. Does svyttest have this functionality, or is there another function I could potentially use?
Thank you!

Consider autocorrelation in a Linear Quantile mixed models (LQMM)

(I am using R and the lqmm package)
I was wondering how to consider autocorrelation in a Linear Quantile mixed models (LQMM).
I have a data frame that looks like this:
df1<-data.frame( Time=seq(as.POSIXct("2017-11-13 00:00:00",tz="UTC"),
as.POSIXct("2017-11-13 00:1:59",tz="UTC"),"sec"),
HeartRate=rnorm(120, mean=60, sd=10),
Treatment=rep("TreatmentA",120),
AnimalID=rep("ID01",120),
Experiment=rep("Exp01",120))
df2<-data.frame( Time=seq(as.POSIXct("2017-08-11 00:00:00",tz="UTC"),
as.POSIXct("2017-08-11 00:1:59",tz="UTC"),"sec"),
HeartRate=rnorm(120, mean=62, sd=14),
Treatment=rep("TreatmentB",120),
AnimalID=rep("ID02",120),
Experiment=rep("Exp02",120))
df<-rbind(df1,df2)
head(df)
With:
The heart rates (HeartRate) that are measured every second on some animals (AnimalID). These measures are carried during an experiment (Experiment) with different treatment possible (Treatment). Each animal (AnimalID) was observed for multiple experiments with different treatments. I wish to look at the effect of the variable Treatment on the 90th percentile of the Heart Rates but including Experiment as a random effect and consider the autocorrelation (as heart rates are taken every second). (If there is a way to include AnimalID as random effect as well it would be even better)
Model for now:
library(lqmm)
model<-lqmm(fixed= HeartRate ~ Treatment, random= ~1| Exp01, data=df, tau=0.9)
Thank you very much in advance for your help.
Let me know if you need more information.
For resources on thinking about this type of problem you might look at chapters 17 and 19 of Koenker et al. 2018 Handbook of Quantile Regression from CRC Press. Neither chapter has nice R code to go from, but they discuss different approaches to the kind of data you're working with. lqmm does use nlme machinery, so there may be a way to customize the covariance matrices for the random effects, but I suspect it would be easiest to either ask for help from the package author or to do a deep dive into the package code to figure out how to do that.
Another resource is the quantile regression model for mixed effects models accounting for autocorrelation in 'Quantile regression for mixed models with an application to examine blood pressure trends in China' by Smith et al. (2015). They model a bivariate response with a copula, but you could do the simplified version with univariate response. I think their model only at this points incorporates lag-1 correlation structure within subjects/clusters. The code for that model does not seem to be available online either though.

Resources