I am working with the nlWaldtest function in R and need help with dynamically setting the ‘texts’ argument based on custom expressions. My objective is to compute Wald tests for each element of a computed vector, kappa_prime, without evaluating it outside the Wald test function.
Here’s my R code for computing Wald tests on elements derived from a custom expression kappa_prime_expr
. I am encountering issues with dynamically indexing into the computed vector within the nlWaldtest
function. I’d appreciate any insights on how to correctly reference individual elements for the test.
your text
kappa_prime_expr <- "(c(b[11], b[12], b[13]) + c(b[14], b[15], b[16])) %*% solve((1 - b[10]) * diag(3) - t(matrix(c(b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9]), nrow = 3)))"
# Custom names for parameters
custom_names <- c("x", "cond8", "cond9a")
# List to store results for the current combination
current_results <- list()
# Initialize kappa_prime calculations
current_results$kappa_prime <- list()
for (j in seq_along(kappa_prime_expr)) {
waldKappa <- nlWaldtest(
texts = paste0("kappa_prime_expr[", j, "]=0"),
coef = coefs_row_vector,
Vcov = as.matrix(vcov_matrix)
)
cKappa <- waldKappa[["statistic"]][["Chisq"]]
sKappa <- sqrt(kappa_prime[j]^2 / cKappa)
pKappa <- waldKappa[["p.value"]][[1]]
current_results$kappa_prime[[custom_names[j]]] <- list(
confint = nlConfint(coef = coefs_row_vector, Vcov = as.matrix(vcov_matrix), texts = paste0("kappa_prime[", j, "]")),
cKappa = cKappa,
sKappa = sKappa,
pvalue = pKappa
)
}
`your text`
I am facing an issue where the texts argument in nlWaldtest does not correctly correspond to the actual elements of the kappa_prime vector. I need to ensure that each texts argument dynamically corresponds to each element derived from the expression in kappa_prime_expr, respecting the order and names assigned in custom_names.
How can I modify the texts argument to dynamically incorporate expressions from kappa_prime_expr for accurate calculations in nlWaldtest? This is crucial as I cannot compute kappa_prime outside of the Wald function due to constraints of the function.
Any suggestions on how to achieve this would be greatly appreciated!
I've been experimenting with different methods to dynamically generate the 'texts' argument in the nlWaldtest function using paste0 and similar functions. I intended to use expressions like paste0("(", kappa_prime_expr, ")[", j, "]") to index and reference each specific element of the kappa_prime vector for the Wald test calculations. My goal was to ensure that calculations were performed on individual elements, not the entire vector. However, this indexing approach hasn't worked as expected. The function interprets these expressions as strings rather than evaluating them as parts of the vector, which means the tests are not applied to the intended individual elements.