Skip to content

Commit

Permalink
gt output for marginal covariance model
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-messier committed Dec 6, 2024
1 parent c3937f4 commit a1c030d
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions R/PrestoGP_Model.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,91 @@ setMethod(
}
)


#' summary table for PrestoGP models
#'
#' This method prints a summary of a PrestoGP model and its parameters using gt package
#'
#' @param object The PrestoGP model object
#'
#' @seealso \code{\link{PrestoGPModel-class}}, \code{\link{prestogp_fit}}
#'
#' @export
#'
#' @examples
#' data(soil)
#' soil <- soil[!is.na(soil[,5]),] # remove rows with NA's
#' y <- soil[,4] # predict moisture content
#' X <- as.matrix(soil[,5:9])
#' locs <- as.matrix(soil[,1:2])
#'
#' soil.vm <- new("VecchiaModel", n_neighbors = 10)
#' soil.vm <- prestogp_fit(soil.vm, y, X, locs)
#' show(soil.vm)

setMethod(
"summary", "PrestoGPModel",
function(object) {
theta_mdl <- get_theta(object)
beta_mdl <- get_beta(object)

Check warning on line 305 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=305,col=5,[object_usage_linter] local variable 'beta_mdl' assigned but may not be used



# Extract the marginal model parameters

Check warning on line 309 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=309,col=0,[indentation_linter] Indentation should be 4 spaces but is 0 spaces.
sigma <- theta_mdl$sigma
smoothness <- theta_mdl$smoothness
nuggets <- theta_mdl$nuggets
names(sigma) <- names(smoothness) <- names(nuggets) <- names(theta_mdl$sigma)

# Scales

Check warning on line 315 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=315,col=0,[indentation_linter] Indentation should be 4 spaces but is 0 spaces.
scale_dims <- length(unique(object@scaling))
scale_df <- data.frame(theta_mdl$scale, id = rep(1:scale_dims, each = length(sigma)))
scales <- scale_df%>%

Check warning on line 318 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=318,col=19,[infix_spaces_linter] Put spaces around all infix operators.
group_by(id) %>%

Check warning on line 319 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=319,col=3,[object_usage_linter] no visible global function definition for 'group_by'

Check warning on line 319 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=319,col=12,[object_usage_linter] no visible binding for global variable 'id'
group_split(.keep = FALSE)

Check warning on line 320 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=320,col=3,[object_usage_linter] no visible global function definition for 'group_split'

Check warning on line 320 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=320,col=29,[trailing_whitespace_linter] Trailing whitespace is superfluous.




# Construct model equations dynamically

Check warning on line 325 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=325,col=0,[indentation_linter] Indentation should be 4 spaces but is 0 spaces.
model_equations <- sapply(seq_along(sigma), function(i) {

Check warning on line 326 in R/PrestoGP_Model.R

View workflow job for this annotation

GitHub Actions / lint

file=R/PrestoGP_Model.R,line=326,col=1,[object_usage_linter] local variable 'model_equations' assigned but may not be used
glue(
"Cᵢᵢ(h) = σᵢᵢ² × M(h | {round(smoothness[i], 2)},[{paste(round(scales[[i]], 2), collapse = ', ')}]) + {round(nuggets[i]^2,4)}"
)
})

# Create the table
marginal_df <- data.frame(
Variable = names(sigma),
Sigma2 = sigma^2, # Avoid special characters
Smoothness = smoothness,
Nugget2 = nuggets^2, # Avoid special characters
Scales = sapply(scales, function(x) paste(round(x, 2), collapse = ", "))
)

# Render as a gt table
marginal_df %>%
gt() %>%
cols_label(
Variable = "Variable",
Sigma2 = "Marginal Variance (σᵢᵢ²)", # Properly labeled
Smoothness = "Smoothness (νᵢᵢ)",
Nugget2 = "nugget (τ²)",
Scales = "scales (α)"
) %>%
fmt_number(
columns = c(Sigma2, Smoothness, Nugget2, Scales),
decimals = 4 # Show 3 significant digits
) |>
tab_header(
title = "Marginal Models for Each Variable",
subtitle = "Cᵢᵢ(h) = σᵢᵢ² × M(h | νᵢᵢ, α)"
)


}
)

#' Extract the Matern (theta) parameters from a PrestoGP model.
#'
#' This method extracts a list containing the Matern variance/covariance
Expand Down

0 comments on commit a1c030d

Please sign in to comment.