I'm trying to productionize the `segmented` package in `r` so that multiple people can use it through `alteryx`. I bring in the strings
the_data_seg_z <- "seg.z ="
seg_data_first_part <-
c(credit_tier_bucket+insured_age_bucket+vehicle_length_bucket+rba_bucket)
and create a formula using
seg_formula <- as.formula(paste(the_data_seg_z, seg_data_first_part, sep = "
~ "))
That seems to run without error. I also bring in a string for the psi as
seg_data_psi <- c(credit_tier_bucket= c(3), insured_age_bucket= c(33),
vehicle_length_bucket= c(28), rba_bucket= c(3000, 18000))
my final line of code is:
piecewise_control <- seg.control(n.boot = 0)
piecewise_seg2 <- segmented(piecewise2, seg_formula, psi =
list(seg_data_psi), control = piecewise_control)
I then get an error that states `wrong number of terms in seg.Z or psi` but the terms match each other.
I feel like it's not reading the "psi" string correctly but I'm not sure the best way to bring it in.
Solved! Go to Solution.
Hi @jbh1128d1,
Does this code function as expected in R Studio? Are you importing the segmented package in R code in your R tool? Can you please include a screenshot of your messages, and your complete code? I would be happy to try troubleshooting this with you.
Thanks!
Hi @SydneyF. Thank you for replying. I just got the answer after asking the same 'type' of question in regards to @Dr.Dan's r-based macro series on there. It does function in RStudio and the segmented packages is installed and loaded during the r-tool process. The problem is r was evaluating this a string. I needed to use the 'eval(parse(text = paste0" command to get R to read correctly.
OLD CODE:
the_data_seg_z <- "seg.z ="
seg_data_first_part <-
c(credit_tier_bucket+insured_age_bucket+vehicle_length_bucket+rba_bucket)
and create a formula using
seg_formula <- as.formula(paste(the_data_seg_z, seg_data_first_part, sep = "
~ "))
That seems to run without error. I also bring in a string for the psi as
seg_data_psi <- c(credit_tier_bucket= c(3), insured_age_bucket= c(33),
vehicle_length_bucket= c(28), rba_bucket= c(3000, 18000))
my final line of code is:
piecewise_control <- seg.control(n.boot = 0)
piecewise_seg2 <- segmented(piecewise2, seg_formula, psi =
list(seg_data_psi), control = piecewise_control)
NEW CODE:
seg_data <- read.Alteryx("#3", mode="data.frame")
seg_data_seg_z <- seg_data$piecewise_formula_seg_z
seg_data_first_part <- seg_data$piecewise_formula_first_part
seg_data_psi <- seg_data$piecewise_formula_psi
seg_formula <- paste(seg_data_seg_z, seg_data_first_part, sep = " ~ ")
seg_formula2 <- paste(seg_data_psi)
#Get bootstrap folds?
boot <- '%Question.How many bootstrap folds?%'
piecewise_control <- seg.control(n.boot = 0)
#run segmented formula
piecewise_seg2 <- eval(parse(text = paste0("segmented(piecewise2, ",seg_formula,", ",seg_data_psi,"),control = piecewise_control)")))