Skip to content
Prev 299726 / 398503 Next

estimation of NA by predict command

HI,

This is just an example as I was not able to read your dataset.? I guess this should work.? My only doubt is whether the independent variable (Year) which is of class "Date" needs to be converted to some other class.? Other than that, it works fine.


Try this:
Year<-c(rep("12.05.1961",20),rep("12.06.1961",20),rep("12.07.1961",20),rep("12.08.1961",10))

dat5<-data.frame(Year,Discharge=dat1[,2])
dat5$Year<-as.Date(dat5[,1],format="%d.%m.%Y")
lm5<-lm(Discharge~Year,dat5)
dat6<-data.frame(Year=dat5[,1])
dat5$fit<-predict(lm5,newdata=dat6)
dat5<-within(dat5,{Dischargenew<-ifelse(is.na(Discharge)==T,fit,Discharge)})
dat5new<-dat5[,c(1:2,4)]
?tail(dat5new,10)
???????? Year Discharge Dischargenew
61 1961-08-12????? 1.08???? 1.080000
62 1961-08-12????? 1.08???? 1.080000
63 1961-08-12????? 1.08???? 1.080000
64 1961-08-12????? 0.65???? 0.650000
65 1961-08-12????? 0.65???? 0.650000
66 1961-08-12????? 0.65???? 0.650000
67 1961-08-12??????? NA???? 1.020796
68 1961-08-12????? 0.44???? 0.440000
69 1961-08-12????? 0.44???? 0.440000
70 1961-08-12????? 0.44???? 0.440000

A.K.