Re: [R] back tick names with predict function
On 12/1/2023 11:47 AM, peter dalgaard wrote: Also, and possibly more constructively, when you get an error like CI.c = predict(mod2, data.frame( `plant-density` = x), interval = 'c') # fail Error in eval(predvars, data, env) : object 'plant-density' not found you should check your assumptions. Does "newdata" actually contain a columnn called "plant-density": Great advice/strategy. Thanks! head(data.frame( `plant-density` = x)) plant.density 1 65.0 2 65.11912 3 65.23824 4 65.35736 5 65.47648 6 65.59560 I.e., it doesn't. So check help for data.frame and looking for something with names. On 1 Dec 2023, at 01:47 , Bert Gunter wrote: "Thank you Rui. I didn't know about the check.names = FALSE argument. Another good reminder to always read help, but I'm not sure I understood what help to read in this case" ?data.frame , of course, which says: "check.names logical. If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names and are not duplicated. If necessary they are adjusted (by make.names) so that they are. " -- Bert __ R-help@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. __ R-help@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.
Re: [R] back tick names with predict function
Also, and possibly more constructively, when you get an error like > CI.c = predict(mod2, data.frame( `plant-density` = x), interval = 'c') # fail Error in eval(predvars, data, env) : object 'plant-density' not found you should check your assumptions. Does "newdata" actually contain a columnn called "plant-density": > head(data.frame( `plant-density` = x)) plant.density 1 65.0 2 65.11912 3 65.23824 4 65.35736 5 65.47648 6 65.59560 > I.e., it doesn't. So check help for data.frame and looking for something with names. > On 1 Dec 2023, at 01:47 , Bert Gunter wrote: > > "Thank you Rui. I didn't know about the check.names = FALSE argument. >> Another good reminder to always read help, but I'm not sure I understood >> what help to read in this case" > > ?data.frame , of course, which says: > > "check.names > > logical. If TRUE then the names of the variables in the data frame are > checked to ensure that they are syntactically valid variable names and > are not duplicated. If necessary they are adjusted (by make.names) so > that they are. " > > -- Bert > > __ > R-help@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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@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.
Re: [R] back tick names with predict function
"Thank you Rui. I didn't know about the check.names = FALSE argument. > Another good reminder to always read help, but I'm not sure I understood > what help to read in this case" ?data.frame , of course, which says: "check.names logical. If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names and are not duplicated. If necessary they are adjusted (by make.names) so that they are. " -- Bert __ R-help@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.
Re: [R] back tick names with predict function
Thank you Rui. I didn't know about the check.names = FALSE argument. Another good reminder to always read help, but I'm not sure I understood what help to read in this case. Since your clue, I've discovered that a tibble-based strategy could also work. x = seq(min(cob_wt$`plant-density`), max(cob_wt$`plant-density`), length = 999) xvals = tibble(`plant-density` = x) CI.c = predict(mod2, newdata = xvals, interval = 'c') CI.p = predict(mod2, newdata = xvals, interval = 'p') Again, appreciate the solution. On 11/30/2023 12:03 PM, Rui Barradas wrote: Às 17:57 de 30/11/2023, Rui Barradas escreveu: À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@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 Hello, Sorry for the comments '# fail' in the last two instructions, I should have changed them. CI.c <- predict(mod2, newdata = newd, interval = 'confidence') # works CI.p <- predict(mod2, newdata = newd, interval = 'prediction') # works Hoep this helps, Rui Barradas __ R-help@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.
Re: [R] back tick names with predict function
Às 17:57 de 30/11/2023, Rui Barradas escreveu: À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@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 Hello, Sorry for the comments '# fail' in the last two instructions, I should have changed them. CI.c <- predict(mod2, newdata = newd, interval = 'confidence') # works CI.p <- predict(mod2, newdata = newd, interval = 'prediction') # works Hoep 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 __ R-help@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.
Re: [R] back tick names with predict function
À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@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 __ R-help@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.
[R] back tick names with predict function
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@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.