Changing Variable Labels in 'ctree' plot

两盒软妹~` 提交于 2020-12-15 07:19:09

问题


I'm struggling to try to make a CI tree from the 'party' package in R look presentable. So far this is what I have, in terms of cleaning the tree up.

current version of tree

What I would like to do is change the variable names on each individual node to something more descriptive and not just the shortened variable names used for coding. I tried changing the variable names themselves using the following code:

colnames(dat.long.imp.m)[colnames(dat.long.imp.m)=="Gender"] <- "sex"

Because some of the variable names contained spaces, I had to enter them into the 'ctree' function the following way:

ctree(formula = DV ~ "`Executive Copartisan`" 

But this gave me an error: Error: attempt to use zero-length variable name. So instead I tried to use the 'apply_labels' function from the 'expss' package, but the labels didn't actually get applied in the ctree plot (as can be seen from the screen grab).

Is there any way to change these variable names to something more descriptive that might include spaces?


回答1:


ctree function is from 'party' package which is not labels-aware. To utilize labels in such cases you need use_labels function. See vignette for details - Variables and value labels support. Example:

library(expss)
library(party)
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      am = "Transmission",
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)

res = use_labels(mtcars, 
           ctree(hp ~ cyl + carb, # or hp ~ .
                 controls = ctree_control(minsplit = 1),
                 data = ..data) # ..data is placeholder for 'mtcars' dataset
           )

plot(res)



来源:https://stackoverflow.com/questions/53999073/changing-variable-labels-in-ctree-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!