0

I am attempting to predict new values from a linear model and apply this to a list of 64 items. My linear model is called JAN.LIN and my new values are in a dataset called JAN.FUT. A small reproducible example:

JAN.FUT <- structure(list(file1 = structure(list(x = c(6L, 5L, 15L, 11L, 
14L, 19L, 6L, 16L, 17L, 6L, 13L, 8L, 14L, 14L, 7L, 19L, 4L, 1L, 
11L, 3L, 2L, 12L, 15L, 3L, 5L, 14L, 2L, 12L, 13L, 1L, 7L, 5L, 
8L, 3L, 19L, 5L, 15L, 13L, 14L, 20L), y = c(29L, 23L, 17L, 14L, 
3L, 5L, 24L, 22L, 16L, 21L, 28L, 52L, 28L, 43L, 33L, 60L, 28L, 
18L, 11L, 9L, 30L, 15L, 17L, 8L, 44L, 19L, 57L, 59L, 45L, 30L, 
9L, 13L, 1L, 60L, 39L, 21L, 35L, 50L, 3L, 44L)), .Names = c("x", 
"y")), file2 = structure(list(x = c(11L, 3L, 11L, 5L, 8L, 7L, 
6L, 18L, 8L, 17L, 7L, 15L, 19L, 3L, 10L, 12L, 13L, 2L, 9L, 10L, 
15L, 13L, 3L, 6L, 16L, 1L, 20L, 5L, 9L, 4L, 12L, 1L, 6L, 13L, 
18L, 7L, 18L, 19L, 15L, 13L), y = c(56L, 31L, 40L, 43L, 20L, 
45L, 55L, 8L, 43L, 26L, 7L, 52L, 7L, 31L, 11L, 14L, 55L, 26L, 
4L, 42L, 34L, 44L, 12L, 4L, 30L, 60L, 23L, 44L, 29L, 55L, 6L, 
37L, 11L, 14L, 36L, 52L, 28L, 22L, 31L, 33L)), .Names = c("x", 
"y"))), .Names = c("file1", "file2"))

> JAN.LIN
$file1

Call:
lm(formula = x$y ~ x$x)

Coefficients:
(Intercept)          x$x  
-92.372        1.016  

--------------------------------------
$file64

Call:
lm(formula = x$y ~ x$x)

Coefficients:
(Intercept)          x$x  
-64.2104       0.9928 

I am attempting to use:

PRED=lapply(JAN.FUT, function(x) predict(JAN.LIN, x)

but this gives the following error:

 Error in UseMethod("predict") : 
 no applicable method for 'predict' applied to an object of class "list" 

Any ideas?

DJ-AFC
  • 569
  • 6
  • 16
  • Could you please provide the previous steps so that this is a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – cocquemas Oct 25 '15 at 19:18
  • @hfty - I have attempted to provide a reproducible example above – DJ-AFC Oct 25 '15 at 19:27
  • try `lapply(JAN.FUT, function(x) predict(JAN.LIN, data.frame(x)))` – Robert Hijmans Oct 25 '15 at 19:31
  • JAN.LIN is a linear model. I have shown it in the example above now. – DJ-AFC Oct 25 '15 at 19:34
  • Thanks @RobertH, but what you propose gives me the error: Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "list" – DJ-AFC Oct 25 '15 at 19:47
  • Anyone have any other ideas? Still can't figure this out. – DJ-AFC Oct 26 '15 at 09:50

1 Answers1

0

Why not use predict with the newdata:

attach(faithful)     # attach the data frame 
eruption.lm = lm(eruptions ~ waiting)
newdata = data.frame(waiting=80) 
predict(eruption.lm, newdata, interval="predict") 
detach(faithful)     # clean up 

You would only have to use your desired values for the newdata instead of the 80 from the example.


As shown here

Konrad
  • 17,740
  • 16
  • 106
  • 167
  • Thanks @konrad, but this is just applying predict to a single dataframe, but I need to apply it to a list of 64 items (64 separate models and 64 separate dataframes with new values). – DJ-AFC Oct 25 '15 at 20:10