Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix small bug and lint function #508

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions R/CyclopsModels.R
Original file line number Diff line number Diff line change
Expand Up @@ -291,54 +291,54 @@
}

predictCyclopsType <- function(coefficients, population, covariateData, modelType = "logistic") {
if (!(modelType %in% c("logistic", "poisson", "survival","cox"))) {
if (!(modelType %in% c("logistic", "poisson", "survival", "cox"))) {
stop(paste("Unknown modelType:", modelType))
}
if (!FeatureExtraction::isCovariateData(covariateData)){
if (!FeatureExtraction::isCovariateData(covariateData)) {
stop("Needs correct covariateData")
}

intercept <- coefficients$betas[coefficients$covariateId%in%'(Intercept)']
if(length(intercept)==0) intercept <- 0
betas <- coefficients$betas[!coefficients$covariateIds%in%'(Intercept)']
intercept <- coefficients$betas[coefficients$covariateIds %in% "(Intercept)"]
if (length(intercept) == 0) intercept <- 0

Check warning on line 302 in R/CyclopsModels.R

View check run for this annotation

Codecov / codecov/patch

R/CyclopsModels.R#L302

Added line #L302 was not covered by tests
betas <- coefficients$betas[!coefficients$covariateIds %in% "(Intercept)"]
coefficients <- data.frame(beta = betas,
covariateId = coefficients$covariateIds[coefficients$covariateIds!='(Intercept)']
covariateId = coefficients$covariateIds[coefficients$covariateIds != "(Intercept)"]
)
coefficients <- coefficients[coefficients$beta != 0, ]
if(sum(coefficients$beta != 0)>0){
if (sum(coefficients$beta != 0) > 0) {
covariateData$coefficients <- coefficients
on.exit(covariateData$coefficients <- NULL, add = TRUE)

prediction <- covariateData$covariates %>%
dplyr::inner_join(covariateData$coefficients, by= 'covariateId') %>%
dplyr::mutate(values = .data$covariateValue*.data$beta) %>%
dplyr::inner_join(covariateData$coefficients, by = "covariateId") %>%
dplyr::mutate(values = .data$covariateValue * .data$beta) %>%
dplyr::group_by(.data$rowId) %>%
dplyr::summarise(value = sum(.data$values, na.rm = TRUE)) %>%
dplyr::select("rowId", "value")

prediction <- as.data.frame(prediction)
prediction <- merge(population, prediction, by ="rowId", all.x = TRUE, fill = 0)
prediction <- merge(population, prediction, by = "rowId", all.x = TRUE, fill = 0)
prediction$value[is.na(prediction$value)] <- 0
prediction$value <- prediction$value + intercept
} else{
warning('Model had no non-zero coefficients so predicted same for all population...')
} else {
warning("Model had no non-zero coefficients so predicted same for all population...")

Check warning on line 324 in R/CyclopsModels.R

View check run for this annotation

Codecov / codecov/patch

R/CyclopsModels.R#L324

Added line #L324 was not covered by tests
prediction <- population
prediction$value <- rep(0, nrow(population)) + intercept
}
if (modelType == "logistic") {
link <- function(x) {
return(1/(1 + exp(0 - x)))
return(1 / (1 + exp(0 - x)))
}
prediction$value <- link(prediction$value)
attr(prediction, "metaData")$modelType <- 'binary'
attr(prediction, "metaData")$modelType <- "binary"
} else if (modelType == "poisson" || modelType == "survival" || modelType == "cox") {

# add baseline hazard stuff

prediction$value <- exp(prediction$value)
attr(prediction, "metaData")$modelType <- 'survival'
if(modelType == "survival"){ # is this needed?
attr(prediction, 'metaData')$timepoint <- max(population$survivalTime, na.rm = T)
attr(prediction, "metaData")$modelType <- "survival"
if (modelType == "survival") { # is this needed?
attr(prediction, "metaData")$timepoint <- max(population$survivalTime, na.rm = TRUE)

Check warning on line 341 in R/CyclopsModels.R

View check run for this annotation

Codecov / codecov/patch

R/CyclopsModels.R#L341

Added line #L341 was not covered by tests
}

}
Expand Down Expand Up @@ -526,4 +526,4 @@
coefs <- data.frame(betas = coefs, covariateIds = rownames(coefs), row.names = NULL)

return(coefs)
}
}
Loading