Predict() for range of observations?

I want to calculate the fitted values for observations who have a council.age (this is the variable name) aged 0 to 20 (continuous variable). I tried the following, and got no error messages:

predict.reelected <- predict(lm3, newdata = data.frame(council.age = 0 - 20, reelected = 1, margin = 10,poverty = 50))

This is probably really stupid but I want to check 0 to 20 has, in fact, between entered as a range and not -20

Indeed, if you try running just this in the R console, you'll see that 0-20 is interpreted as -20:

data.frame(council.age = 0 - 20, reelected = 1, margin = 10,poverty = 50)
#>   council.age reelected margin poverty
#> 1         -20         1     10      50

In R to create a range you need the colon :

data.frame(council.age = 0:20, reelected = 1, margin = 10,poverty = 50)
#>    council.age reelected margin poverty
#> 1            0         1     10      50
#> 2            1         1     10      50
#> 3            2         1     10      50
#> 4            3         1     10      50
#> 5            4         1     10      50
#> 6            5         1     10      50
#> 7            6         1     10      50
#> 8            7         1     10      50
#> 9            8         1     10      50
#> 10           9         1     10      50
#> 11          10         1     10      50
#> 12          11         1     10      50
#> 13          12         1     10      50
#> 14          13         1     10      50
#> 15          14         1     10      50
#> 16          15         1     10      50
#> 17          16         1     10      50
#> 18          17         1     10      50
#> 19          18         1     10      50
#> 20          19         1     10      50
#> 21          20         1     10      50
1 Like

try


council.age = 0:20

if you want each line to have a vector of numbers 1 through 20

UPDATE: ignore above. I really should ignore questions without of a reprex. See the FAQ.

Assuming that you want to take a source data frame x and create a subset y

set.seed(42)
ages <- data.frame(age = sample(0:75, 100, replace = TRUE))
(young <- ages[which(ages$age %in% 0:20),])
#>  [1] 17 19  2  4  2 14  7 17  3  5  5  1  2 20  1  9  4  8 15  1 17 20 17 12  0
#> [26] 12  4 15 13  5
1 Like

(Feel free to ignore if this is not interesting to you but) what motivates you to respond to beginner questions like this? Maybe I'm missing something, but I can't help but see people like you taking the time to respond to questions like this as anything but altruism

Well, the human heart has room for a variety of motivations. For me it goes beyond doing good.

In SF, there was an indie bookstore A Clean, Well Lighted Place for Books . I think of this community as a specialized digital spiritual successor. You can meet interesting people and develop relationships. It well moderated, temperate and respectful. It's nicer to hang here than places like the MuskOx.

Who doesn't like to show off?

Maybe the biggest reason is how much I learn by engaging here—it's much easier to spot flaws in other people's code than one's own. And you never learn a subject well until you teach it.

Thanks for the kind thoughts.

2 Likes

This is a very, very good answer.

Thank you for the self-awareness

1 Like