简体   繁体   中英

R targets packages error - Last error: <text>:1:2: unexpected '/'

When I run the code, there is a error:

✖ error target quantities_plot_base
• end pipeline [3.558 seconds]
Error:

! Error running targets::tar_make()
  
Target errors: targets::tar_meta(fields = error, complete_only = TRUE)
  
Tips: https://books.ropensci.org/targets/debugging.html
  
Last error: <text>:1:2: unexpected '/'

1: +/

     ^

The target is the following code in the list:

tar_target(
    name = quantities_plot_base,
    command = powerscale_seq_summ_plot(sensitivity_sequence_base)
  )

And the powerscale_seq_summ_plot in the function file is like:

powerscale_seq_summ_plot <- function(powerscale_seq) {

  powerscale_plot_quantities(powerscale_seq, variables = "b_wrist", quantities = c("mean", "sd"), mcse = TRUE) +
    ggplot2::facet_wrap(
      . ~ quantity,
      scales = "free",
      ncol = 3,
      labeller = ggplot2::as_labeller(
        c(
          "b_wrist" = "",
          "sd" = "SD",
          "mean" = "Mean",
          "cjs_dist" = "$\\text{CJS}_{\\text{dist}}$"
        )
      )
    ) +
    guides(colour = "none") +
    xlab("Power-scaling $\\alpha$") +
    scale_color_manual(values = rep("black", 3)) +
    scale_shape_manual(values = c("prior" = 15, "likelihood" = 22), labels = c("Prior power-scaling", "Likelihood power-scaling"), name = NULL) +
    scale_linetype_manual(values = "dashed", labels = "$\\pm2$ MCSE", name = NULL) +
    cowplot::theme_half_open() +
    theme(
      legend.position = "bottom",
      legend.text = element_text(size = 10),
      axis.text = element_text(size = 10),
      axis.title = element_text(size = 10),
      strip.text = element_text(size = 10),
      strip.background = element_blank(),
      legend.text.align = 0,
      axis.line.y = element_blank(),
      axis.ticks.y = element_line(colour = "black"),
      axis.line.x = element_blank(),
      axis.ticks.x = element_line(colour = "black"),
      legend.title = element_text(size = 10),
      aspect.ratio = 1
    ) +
    cowplot::panel_border(color = "black", size = 1)
}

Error Traceback plot I am not sure which '+' conducts the error.

options code for $ $ tikzLatex, it doesn't work on my Rstudio, is there any wrong?

options(
  tizkDocumentDelcaration = "\\documentclass[10pt]{article}",
  tikzLatexPackages = c(
    getOption("tikzLatexPackages"),
    "\\usepackage{tikz}",
    "\\usepackage{amsmath}",
    "\\usepackage{amsfonts}",
    "\\usepackage{bm}",
    "\\usepackage{lmodern}",
    "\\usepackage{multirow}",
    "\\usepackage[T1]{fontenc}",
    "\\usepackage{textcomp}",
    "\\usepackage{microtype}",
    "\\DeclareMathOperator{\\normal}{normal}",
    "\\DeclareMathOperator{\\Bernoulli}{Bernoulli}",
    "\\DeclareMathOperator{\\gammadist}{gamma}",
    "\\DeclareMathOperator{\\expdist}{exponential}",
    "\\DeclareMathOperator{\\betadist}{beta}",
    "\\DeclareMathOperator{\\Cauchy}{Cauchy}"
  )
)

The code is from the GitHub link . If you are interested in the detail, you can check the case 'bodyfat'.

The problem is with the priorsense package. If you removed mcse = TRUE it would work. Since I figured out what's wrong, I've written to the maintainers of the package. They have almost nothing open, which seems like a really good sign. (As in they fix issues that users raise...I'm assuming...)

While this answer will get you the plot, you'll find that there are other errors in your code. (I'll go into that more at the end.) However, some of the errors I see may be resolved if I used your data.

There are a few things you could do before this issue is fixed in that R package. One possibility is cloning their package in github, fixing the code, and then you would install your repo (which replaces your current priorsense library). An easier method is to add the two functions below in your code to your script file before you call the plot. (These replace the two functions you need to make this work in the priorsense library. These two functions are almost identical to what is literally in the priorsense library with a few changes—I reindented the lines (for easier reading); I removed and replaced deprecated functions; I fixed the code causing the error.

You will not need to change any of the code that you've written. (I'm assuming...you didn't share your data, so there could be other errors.)

library(posterior) # this is needed for these functions to work
          # ggplot2 is also needed, but you're already calling it
powerscale_summary_plot <- function (
    x, variables, quantities = NULL, scale = FALSE, base_mcse = NULL, ...)
{
  if (is.null(quantities)) {
    quantities <- setdiff(colnames(x[[1]]), c("variable", "alpha", "component",
                                              "pareto_k", "pareto_kf", "n_eff"))
  }
  x[[1]] <- x[[1]][x[[1]][["variable"]] %in% variables, ]
  summaries <- stats::reshape(data = x[[1]], varying = quantities, 
                              direction = "long", times = quantities, 
                              v.names = "value", timevar = "quantity")
  summaries$quantity <- factor(summaries$quantity, levels = quantities)
  summaries$pareto_k_value <- ifelse(summaries$pareto_k > 0.5, 
                                     "> 0.5", "< 0.5")
  summaries$pareto_k_value <- factor(summaries$pareto_k_value, 
                                     levels = c("< 0.5", "> 0.5"))
  points <- summaries[summaries$alpha == min(summaries$alpha) | 
                        summaries$alpha == max(summaries$alpha), ]
  p <- ggplot(data = summaries, mapping = aes(x = alpha, y = value)) + 
    geom_line(aes(color = pareto_k_value, group = component)) + 
    facet_wrap(facets = variable ~ quantity, scales = "free", 
               ncol = length(quantities)) + 
    geom_point(aes(x = alpha, y = value, shape = component, 
                   color = pareto_k_value), 
               fill = "white", size = 3, data = points) + 
    scale_shape_manual(values = c(likelihood = 22, prior = 15)) + 
    scale_color_viridis_d(drop = FALSE) + 
    guides(color = guide_legend(title = "pareto-k", 
                                override.aes = list(shape = 15))) + 
    ylab("") + 
    scale_x_continuous(
      trans = "log2", 
      limits = c(min(summaries$alpha) - 0.01, max(summaries$alpha) + 0.01), 
      breaks = c(min(summaries$alpha), 1, max(summaries$alpha)), 
      labels = round(c(min(summaries$alpha), 1, max(summaries$alpha)), 
                     digits = 3)) + 
    ggtitle(
      label = "Power-scaling sensitivity", 
      subtitle = paste0("Posterior quantities depending on amount of power-scaling ",
                        "(alpha).\nHorizontal lines indicate low sensitivity.\nSteeper ",
                        "lines indicate greater sensitivity.\nEstimates with Pareto-k ",
                        "values > 0.5 may be inaccurate.")) + 
    theme(aspect.ratio = 1)
  if (!is.null(base_mcse)) {
    bmcse <<- base_mcse
    p <- p + scale_linetype_manual(values = "dashed", name = NULL) + 
      geom_hline(aes(yintercept = `mcse_min`, linetype = "+/-2MCSE"),
                 data = base_mcse, color = "black") + 
      geom_hline(aes(yintercept = `mcse_max`, linetype = "+/-2MCSE"), 
                 data = base_mcse, color = "black")
  }
  
  return(p)
}


powerscale_plot_quantities <- function (
    x, variables,quantities = c("mean", "median", "sd", "mad", "quantile2"), 
    div_measure = "cjs_dist", resample = FALSE, measure_args = NULL, mcse = FALSE, ...)
{
  summ <- summarise_draws(x, ... = quantities, resample = resample, 
                          div_measures = div_measure, 
                          measure_args = measure_args)
  variables <- variables(
    subset_draws(x[[1]], variable = variables))
  if (mcse) {
    quants <- setdiff(colnames(summ[[1]]), 
                      c("variable", "alpha", "component", "pareto_k", 
                        "pareto_kf", "n_eff"))
    base_quantities <- summ[[1]][which(summ[[1]]$alpha == 1), ]
    base_quantities <- unique(base_quantities[c("variable", quants)])
    base_mcse <- summarise_draws(x$base_draws, 
                                 default_mcse_measures())
    base_mcse <- base_mcse[which(base_mcse$variable %in% 
                                   variables), ]
    base_mcse <- as.data.frame(base_mcse)
    base_mcse <- stats::reshape(data = base_mcse, 
                                varying = c("mcse_mean", "mcse_median", 
                                            "mcse_sd", "mcse_q5", "mcse_q95"),
                                direction = "long", 
                                times = c("mean", "median", "sd", "q5", "q95"),
                                v.names = "mcse", timevar = "quantity", 
                                idvar = "variable")
    base_q <- as.data.frame(base_quantities)
    base_q <- stats::reshape(data = base_q, varying = quants, 
                             direction = "long", times = quants, 
                             v.names = "value", 
                             timevar = "quantity", idvar = "variable")
    base_mcse <- merge(base_q, base_mcse)
    base_mcse$mcse_min <- base_mcse$value - 2 * base_mcse$mcse
    base_mcse$mcse_max <- base_mcse$value + 2 * base_mcse$mcse
  }
  else {
    base_mcse <- NULL
  }
  return(powerscale_summary_plot(summ, variables = variables, 
                                 base_mcse = base_mcse, ...))
}

I used their example fit from their Github site to test this.

powerscale_plot_quantities(pss, variables = "mu", 
                           quantities = c("mean", "sd"), mcse = TRUE)

在此处输入图像描述

Using your function (With b_wrist swapped for mu ; I made the variable call another argument in the function).

powerscale_seq_summ_plot(pss, "mu")
# Scale for colour is already present.
# Adding another scale for colour, which will replace the existing scale.
# Scale for shape is already present.
# Adding another scale for shape, which will replace the existing scale.
# Scale for linetype is already present.
# Adding another scale for linetype, which will replace the existing scale. 

在此处输入图像描述

I don't know of any libraries that let you use $ encapsulated expressions that ggplot2 will understand. If these work for you, have at it, Otherwise. here are some solutions to the label errors I see in the plot above.

For "cjs_dist" = "$\\text{CJS}_{\\text{dist}}$" I don't know what you're trying to do here. Are you just trying to show "CJS_dist" instead of "cjs_dist"? If so, then be literal: "cjs_dist" = "CJS_dist" .

For xlab("Power-scaling $\\alpha$") + use xlab(expression(Power-scaling~alpha)) +

For scale_l.netype_manual(values = "dashed", labels = "$\\pm2$ MCSE", name = NULL) + replace the labels with "±2 MCSE"

When I made these changes in your UDF (user-defined function) this is what I rendered.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM