|
| 1 | + |
| 2 | +c |
| 3 | + |
| 4 | + |
| 5 | +#' |
| 6 | +#' @title Heterogeneous Correlation Matrix |
| 7 | +#' @description This function is based on the hetcor function from the R package \code{polycor}. |
| 8 | +#' @details Computes a heterogenous correlation matrix, consisting of Pearson product-moment |
| 9 | +#' correlations between numeric variables, polyserial correlations between numeric and ordinal |
| 10 | +#' variables, and polychoric correlations between ordinal variables. |
| 11 | +#' @param data the name of a data frame consisting of factors, ordered factors, logical variables, |
| 12 | +#' character variables, and/or numeric variables, or the first of several variables. |
| 13 | +#' @param ML if TRUE, compute maximum-likelihood estimates; if FALSE (default), compute quick |
| 14 | +#' two-step estimates. |
| 15 | +#' @param std.err if TRUE (default), compute standard errors. |
| 16 | +#' @param bins number of bins to use for continuous variables in testing bivariate normality; |
| 17 | +#' the default is 4. |
| 18 | +#' @param pd if TRUE (default) and if the correlation matrix is not positive-definite, an attempt |
| 19 | +#' will be made to adjust it to a positive-definite matrix, using the nearPD function in the Matrix |
| 20 | +#' package. Note that default arguments to nearPD are used (except corr=TRUE); for more control call |
| 21 | +#' nearPD directly. |
| 22 | +#' @param use if "complete.obs", remove observations with any missing data; if "pairwise.complete.obs", |
| 23 | +#' compute each correlation using all observations with valid data for that pair of variables. |
| 24 | +#' @param datasources a list of \code{\link[DSI]{DSConnection-class}} objects obtained after login. |
| 25 | +#' If the \code{datasources} argument is not specified the default set of connections will be |
| 26 | +#' used: see \code{\link[DSI]{datashield.connections_default}}. |
| 27 | +#' @return Returns an object of class "hetcor" from each study, with the following components: the |
| 28 | +#' correlation matrix; the type of each correlation: "Pearson", "Polychoric", or "Polyserial"; the |
| 29 | +#' standard errors of the correlations, if requested; the number (or numbers) of observations on which |
| 30 | +#' the correlations are based; p-values for tests of bivariate normality for each pair of variables; |
| 31 | +#' the method by which any missing data were handled: "complete.obs" or "pairwise.complete.obs"; TRUE |
| 32 | +#' for ML estimates, FALSE for two-step estimates. |
| 33 | +#' @author Demetris Avraam for DataSHIELD Development Team |
| 34 | +#' @export |
| 35 | +#' |
| 36 | +ds.hetcor <- function(data=NULL, ML=TRUE, std.err=TRUE, bins=4, pd=TRUE, use="complete.obs", datasources=NULL){ |
| 37 | + |
| 38 | + # look for DS connections |
| 39 | + if(is.null(datasources)){ |
| 40 | + datasources <- datashield.connections_find() |
| 41 | + } |
| 42 | + |
| 43 | + # ensure datasources is a list of DSConnection-class |
| 44 | + if(!(is.list(datasources) && all(unlist(lapply(datasources, function(d) {methods::is(d,"DSConnection")}))))){ |
| 45 | + stop("The 'datasources' were expected to be a list of DSConnection-class objects", call.=FALSE) |
| 46 | + } |
| 47 | + |
| 48 | + if(is.null(data)){ |
| 49 | + stop("Please provide the name of the input object!", call.=FALSE) |
| 50 | + } |
| 51 | + |
| 52 | + # check if the input object is defined in all the studies |
| 53 | + defined <- isDefined(datasources, data) |
| 54 | + |
| 55 | + calltext <- call('hetcorDS', data, ML, std.err, bins, pd, use) |
| 56 | + output <- DSI::datashield.aggregate(datasources, calltext) |
| 57 | + |
| 58 | + return(output) |
| 59 | + |
| 60 | +} |
0 commit comments