简体   繁体   中英

Predict new values from a linear model for a list

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM