[R] back tick names with predict function

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Nov 30 18:57:07 CET 2023


Às 17:38 de 30/11/2023, Robert Baer escreveu:
> 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
> *
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

When creating the new data df, the default check.names = TRUE changes 
the column name, it is repaired and the hyphen is replaced by a legal dot.


# check.names defaults to TRUE
newd <- data.frame(`plant-density` = x)
# `plant-density` is not a column name
head(newd)

# check.names set to FALSE
newd <- data.frame(`plant-density` = x, check.names = FALSE)
# `plant-density` is becomes a column name
head(newd)


# 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, newdata = newd, interval = 'confidence')  # 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, newdata = newd, interval = 'prediction')    # fail



Hope this helps,

Rui Barradas


-- 
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
www.avg.com



More information about the R-help mailing list