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 ?
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
...