[R-sig-Geo] Interpolating

Tom Gottfried tom.gottfried at o2mail.de
Mon Aug 20 23:09:38 CEST 2012


 From ?predict.lm:
"Variables are first looked for in newdata and then searched for in the 
usual way (which will include the environment of the formula used in the 
fit). A warning will be given if the variables found are not of the same 
length as those in newdata if it was supplied."

The first sentence actually implies, that the columns in newdata need to 
have the names of the variables as specified in the model formula. 
Otherwise, at least in the multivariable case and if the data.frame has 
more than one column, it is impossible to recognize which data to use. Thus

x<-data.frame(day=c(1,2,3,4))

HTH,
Tom

Am 20.08.2012 22:47, schrieb K Jjw:
> Hi
> Here is some of my code
> jan_ppt_i<- structure(c(155.100006103516, 203.931690865494, 218.219503262085
> ), .Names = c("prec_clim_jan", "pr_2040a1b_jan_ukmo_hadcm3",
> "pr_2090a1b_jan_ukmo_hadcm3"))
> day<-c(1,18262,36525)
>
> #linear regression of the data
> linr_jan<-predict(lm(jan_ppt_i~day))
> #prediction where it stops working
> x<-data.frame(c(1,2,3,4))
> pred<-predict(lm(jan_ppt_i~day), x)
>
> Warning message:
> 'newdata' had 4 rows but variable(s) found have 3 rows
>
> Thanks for your help
> KJ
> *From:* Tom Gottfried <tom.gottfried at o2mail.de>
> *To:* r-sig-geo at r-project.org
> *Sent:* Monday, August 20, 2012 8:16 PM
> *Subject:* Re: [R-sig-Geo] Interpolating
>
> Am 20.08.2012 05:08, schrieb K Jjw:
>  > Hi
>  > I tried predict but I get an error. Warning message: 'newdata' had 4
> rows but variable(s) found have 3 rows
>  > it only works if the number of rows that i am trying to predict are
> the same as the original number of rows (3). It does however work when I
> try to do the same thing for example in ?predict (tried with 17 rows to
> predict when the original function is 15 rows) . Not sure what is going
> on. Has anyone run into the same problem before?
>
> Not sure what is going on neither. But a reproducible example would
> surely help you and others on the list to get to know what is going on.
>
> Regards,
> Tom
>
>  > Thanks
>  > KJ
>  >
>  >
>  >
>  > ?predict
>  > ?predict.lm
>  >
>  > HTH,
>  > Tom
>  >
>  > Am 15.08.2012 04:05, schrieb K Jjw:
>  >> Hi
>  >>
>  >>
>  >> I have data that are from 1990, 2040, and 2090. I
>  > need to loop through each location (defined in a grid of 240x260) and
> find a
>  > linear regression for each place across the 3 years. I then need to
> interpolate
>  > the data to get the missing years for the given location ie I need
> all the data
>  > for 1991, 1992, 1993 etc. I have a loop and I have figured out how to
> make the
>  > model ie lm(). What I am having difficulty with is interpolating the data
>  > without hardcoding in the formula of the linear regression.
>  >>
>  >> so basically the bit of code that is causing me
>  > problems is
>  >>
>  >> linr<-lm(jan_ppt~date)
>  >> linr
>  >>
>  >> Call:
>  >> lm(formula = jan_ppt ~ date)
>  >>
>  >> Coefficients:
>  >> (Intercept) date
>  >> 1.482e+02 1.728e-03
>  >>
>  >> x<- 1990:2090
>  >> y<-1.482e2 + 1.728e-3*x #this is what I have now
>  >> y<- intercept+ grad*x #this is what I want where
>  > 'intercept' and 'grad' are defined from the model 'linr'
>  >>
>  >> how do I
>  > define y without hardcoding the coefficients as they will change for
> every
>  > location. I basically need to do linear regressions for each location
> then
>  > interpolate the data to get yearly data in order to put this data in
> another
>  > model that requires yearly data.
>  >>
>  >> Thanks
>  >>
>  >> KJ
>  >> [[alternative
>  > HTML version deleted]]
>  >>
>  >>
>  >>
>  >>
>  >> _______________________________________________
>  >> R-sig-Geo mailing list
>  >> R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>  >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>  >
>  > _______________________________________________
>  > R-sig-Geo mailing list
>  > R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>  > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>  > [[alternative HTML version deleted]]
>  >
>  >
>  >
>  >
>  > _______________________________________________
>  > R-sig-Geo mailing list
>  > R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>  > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>



More information about the R-sig-Geo mailing list