2019-06-19 20:14:11 +00:00
|
|
|
loadData <- function(data, xVarNames, responses, covariateList.java = NULL){
|
2019-05-31 22:13:24 +00:00
|
|
|
|
|
|
|
if(class(responses) == "integer" | class(responses) == "numeric"){
|
|
|
|
responses <- Numeric(responses)
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:14:11 +00:00
|
|
|
# connectToData provides a pre-created covariate list we can re-use
|
|
|
|
if(is.null(covariateList.java)){
|
|
|
|
covariateList.java <- getCovariateList(data, xVarNames)
|
|
|
|
}
|
2019-05-31 22:13:24 +00:00
|
|
|
|
|
|
|
textColumns <- list()
|
|
|
|
for(j in 1:length(xVarNames)){
|
|
|
|
textColumns[[j]] <- .jarray(as.character(data[,xVarNames[j]]), "S")
|
|
|
|
}
|
|
|
|
textData <- convertRListToJava(textColumns)
|
|
|
|
|
|
|
|
rowList <- .jcall(.class_RUtils, makeResponse(.class_List), "importDataWithResponses",
|
|
|
|
responses$javaObject, covariateList.java, textData)
|
|
|
|
|
|
|
|
return(list(covariateList=covariateList.java, dataset=rowList))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
getCovariateList <- function(data, xvarNames){
|
|
|
|
covariateList <- .jcast(.jnew(.class_ArrayList, length(xvarNames)), .class_List)
|
|
|
|
|
|
|
|
for(i in 1:length(xvarNames)){
|
|
|
|
xName = xvarNames[i]
|
|
|
|
|
|
|
|
column <- data[,xName]
|
|
|
|
|
|
|
|
if(class(column) == "numeric" | class(column) == "integer"){
|
|
|
|
covariate <- Java_NumericCovariate(xName, i-1)
|
|
|
|
}
|
|
|
|
else if(class(column) == "logical"){
|
|
|
|
covariate <- Java_BooleanCovariate(xName, i-1)
|
|
|
|
}
|
|
|
|
else if(class(column) == "factor"){
|
|
|
|
lvls <- levels(column)
|
|
|
|
covariate <- Java_FactorCovariate(xName, i-1, lvls)
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
stop("Unknown column type")
|
|
|
|
}
|
|
|
|
|
|
|
|
.jcall(covariateList, "Z", "add", covariate)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return(covariateList)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
loadPredictionData <- function(newData, covariateList.java){
|
|
|
|
|
|
|
|
xVarNames <- character(.jcall(covariateList.java, "I", "size"))
|
|
|
|
for(j in 1:length(xVarNames)){
|
|
|
|
covariate.java <- .jcast(
|
|
|
|
.jcall(covariateList.java, makeResponse(.class_Object), "get", as.integer(j-1)),
|
|
|
|
.class_Covariate
|
|
|
|
)
|
|
|
|
|
|
|
|
xVarNames[j] <- .jcall(covariate.java, makeResponse(.class_String), "getName")
|
|
|
|
}
|
|
|
|
|
|
|
|
if(any(!(xVarNames %in% names(newData)))){
|
|
|
|
varsMissing = xVarNames[!(xVarNames %in% names(newData))]
|
|
|
|
|
|
|
|
error <- paste0("The following covariates are not present in newdata: ", paste(varsMissing, collapse = ", "))
|
|
|
|
stop(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
textColumns <- list()
|
|
|
|
for(j in 1:length(xVarNames)){
|
|
|
|
textColumns[[j]] <- .jarray(as.character(newData[,xVarNames[j]]), "S")
|
|
|
|
}
|
|
|
|
textData <- convertRListToJava(textColumns)
|
|
|
|
|
|
|
|
rowList <- .jcall(.class_RUtils, makeResponse(.class_List),
|
|
|
|
"importData", covariateList.java, textData)
|
|
|
|
|
|
|
|
|
|
|
|
return(rowList)
|
|
|
|
}
|
|
|
|
|