[R] back tick names with predict function

Robert Baer rb@er @end|ng |rom @t@u@edu
Thu Nov 30 18:38:19 CET 2023


I am having trouble using back ticks with the R extractor function 
'predict' and an lm() model.  I'm trying too construct some nice vectors 
that can be used for plotting the two types of regression intervals.  I 
think it works with normal column heading names but it fails when I have 
"special" back-tick names.  Can anyone help with how I would reference 
these?  Short of renaming my columns, is there a way to accomplish this?

Repex

*# dataframe with dashes in column headings
cob =
   structure(list(`cob-wt` = c(212, 241, 215, 225, 250, 241, 237,
                             282, 206, 246, 194, 241, 196, 193, 224, 
257, 200, 190, 208, 224
), `plant-density` = c(137, 107, 132, 135, 115, 103, 102, 65,
                        149, 85, 173, 124, 157, 184, 112, 80, 165, 160, 
157, 119)),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L))

# regression model works
mod2 = lm(`cob-wt` ~ `plant-density`, data = cob)

# x sequence for plotting CI's
# Set up x points
x = seq(min(cob$`plant-density`), max(cob$`plant-density`), length = 1000)

# Use predict to get CIs for a plot
# Add CI for regression line (y-hat uses 'c')
# usual trick is to assign x to actual x-var name in middle dataframe 
arguement
CI.c = predict(mod2, data.frame( `plant-density` = x), interval = 'c')  
# fail

# Add CI for prediction value (y-tilde uses 'p')
# usual trick is to assign x to actual x-var name in middle dataframe 
arguement
CI.p = predict(mod2, data.frame(`plant-density`  = x), interval = 
'p')    # fail
*



More information about the R-help mailing list