From the setNames help in R:
x <- 1:20
y <- setNames(x + (x/4 - 2)^3 + rnorm(20, sd = 3),
paste("O", x, sep = "."))
ww <- rep(1, 20); ww[13] <- 0
summary(lmxy <- lm(y ~ x + I(x^2)+I(x^3) + I((x-10)^2), weights = ww),
cor = TRUE)
variable.names(lmxy)
variable.names(lmxy, full = TRUE) # includes the last
case.names(lmxy)
case.names(lmxy, full = TRUE) # includes the 0-weight case
If I plot the regression object using:
plot(lmxy, 1)
I can see the case names in the plot. This is what I want.
However, if the data is stored as a tibble:
tt = as.tibble(cbind(x, y, ww))
lmxy_tt = lm(y ~ x + I(x^2)+I(x^3) + I((x-10)^2), weights = ww, data = tt)
plot(lmxy_tt, 1)
The case names are lost.
My data is usually stored in tibbles but I want a way to maintain the case names in the plot. I know tibbles doesn't support row names.
I've tried:
plot(lm(setNames(y1, paste("O", x1, sep = ".")) ~ x1 + I(x1^2)+I(x1^3) + I((x1-10)^2), weights = ww1, data = tt), 1)
To make things clearer, this is a toy example using the mtcars dataset of what I'm trying to achieve:
library(tidyverse)
cars = mtcars
cars$name = row.names(cars)
cars = as.tibble(cars)
cars = cars %>%
group_by(cyl) %>%
nest() %>%
mutate(model = map(data, ~ lm(set_names(wt, name) ~ hp, data = .x)))
Now if I plot the result:
plot(cars$model[[1]], 1)
I can see the case names as '1' and '5' but I want the car models labeled. "Mazda RX4" etc. I want to somehow have the case names saved into the regression model instead of the defaults of '1', '2' etc. using 'setNames', 'set_names' or another method.
Hope this is clearer.
Thanks