1

I am sorry to repeat this question over and over but it seems that I have poor understanding in predicting wider range. It seems that if the data's nrow matches well with predicting values you have no error. However, if you want to predict for different range we will be getting an error.

Using the same data from dplyrdo-requires-named-function

it works well. But if you want to change the range of the fitting I am getting an error!

library(dplyr)
iris %>%
  group_by(Species) %>%
  do({
    mod <- lm(Sepal.Length ~ Sepal.Width, data = .)
    pred <- predict(mod, newdata = data.frame(Sepal.Width=seq(1,10,length.out=51)))
    data.frame(., pred)
  })

Error in data.frame(., pred) : arguments imply differing number of rows: 50, 51

I understand that the new range does not match with the previous data .. OTH, I need to predict for wider range of Sepal.Width values. Is this possible ?

Alexander
  • 4,527
  • 5
  • 51
  • 98

1 Answers1

2

When you use data.frame(.,pred) you're trying to bind together an existing data frame with 50 rows and a new prediction with 51 rows. If you replace this line by data.frame(pred) everything works fine:

# A tibble: 153 x 2
# Groups:   Species [3]
   Species     pred
    <fctr>    <dbl>
 1  setosa 3.329491
 2  setosa 3.453779
 3  setosa 3.578067
 ...
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • That's cool but if you want to plot something with using the same `.` data.frame it is useful to have `data.frame(.,pred)`.In other case I guess we need to build another data from this. For instance, if I wanted to plot `Sepal.Length_predicted` vs `pred` I need to build new `data.frame`. Am I correct? – Alexander Jul 26 '17 at 03:19
  • and this `Sepal.Lenght_predicted` is coming from whatever the fitting coef's that we get from `lm`. – Alexander Jul 26 '17 at 03:25