Reputation: 133
I am trying to fill one column using lm and predict functions. I need to make a new column called "estimation" with 24 new values.
My data looks like:
first second third
17 44 50
788 890 1409
968 218 344
212 185 306
333 355 NA
0 4 10
160 156 184
4 4 8
12 4 4
21 2 0
3 0 0
35 32 NA
22 18 59
97 150 40
1 18 79
12 32 23
42 21 22
8 2 5
24 17 0
4 2 2
0 0 0
29 19 21
15 20 NA
0 1 NA
Firstly, I use the stochastic regression model and the function lm to get b and a coefficients (ax+b). I get them. Next with the function predict, I want to fill the new column "estimation". But I get an error.
data<- X12_5_3
regr <- lm(X12_5_3$third ~ X12_5_3$second)
regr
X12_5_3$estimation<-predict(regr, na.action=TRUE)
X12_5_3$estimation
Error in `$<-.data.frame`(`*tmp*`, estimation, value = c(`1` =
57.4742893243486, :
replacement has 20 rows, data has 24
I do not get this column.
Upvotes: 2
Views: 781
Reputation: 48191
It's not complete clear what your desired outcome is. One the one hand, what you may prefer is
(X12_5_3$estimation <- predict(regr, newdata = X12_5_3))
# [1] 57.474289 1380.179615 329.520065 277.925177 543.717027 -5.064970
# [7] 232.584214 -5.064970 -5.064970 -8.191932 -11.318895 38.712512
# [13] 16.823771 223.203325 16.823771 38.712512 21.514215 -8.191932
# [19] 15.260290 -8.191932 -11.318895 18.387253 19.950734 -9.755414
in which case in addition to 20 fitted values you also get 4 actual predictions from using regr
. On the other hand, if you actually want just fitted values with four NA
's, then
regr <- lm(X12_5_3$third ~ X12_5_3$second, na.action = "na.exclude")
(X12_5_3$estimation <- predict(regr))
# 1 2 3 4 5 6 7
# 57.474289 1380.179615 329.520065 277.925177 NA -5.064970 232.584214
# 8 9 10 11 12 13 14
# -5.064970 -5.064970 -8.191932 -11.318895 NA 16.823771 223.203325
# 15 16 17 18 19 20 21
# 16.823771 38.712512 21.514215 -8.191932 15.260290 -8.191932 -11.318895
# 22 23 24
# 18.387253 NA NA
Upvotes: 1