warning when calculating predicted values

前端 未结 4 1501
耶瑟儿~
耶瑟儿~ 2021-01-05 10:29

working with a data frame

x
    Date      Val
    1/1/2012   7
    2/1/2012   9
    3/1/2012   20
    4/1/2012   24
    5/1/2012   50
a <- seq(as.Date(tai         


        
相关标签:
4条回答
  • 2021-01-05 10:45

    Your variable names, as stored in the x.lm model, refer to the x dataframe. There are no variables of the same names in a, so it will use those 29 from x again, which is probably not what you wanted, thus the warning. You can do the following to always use an unqualified variable named Date in the model:

    a <- seq(as.Date(tail(x, 1)$Date), by="month", length=5)
    a <- data.frame(Date = a)
    x.lm <- lm(Val ~ Date, data=x)
    x.pre<-predict(x.lm, newdata=a)
    
    0 讨论(0)
  • 2021-01-05 10:56

    Instead of x.lm <- lm(x$Val ~ x$Date, data = x) use x.lm <- lm(Val ~ Date, data = x). Removing dataset name before variable name in the lm function should help.

    0 讨论(0)
  • 2021-01-05 10:59

    Your data.frame a has a column named a. You created your model with columns named Val and Date so that is what its looking for.

    when you make your data.frame a name that column Date and you're good to go:

    a <- data.frame(Date=a)
    

    Then it runs without the warning.

    Per comment:

    Edit your lm call to be:

    lm(Val ~ Date, data=x)
    
    0 讨论(0)
  • 2021-01-05 11:08

    If you can't make predict.lm() work, then you should try to write your own function using function():

    yourown_function<- function(predictor1, predictor2,...){intercept+b1*predictor1+b2*predictor2+...}
    

    use yourown_function to predict from any new dataframe:

    newvalues<- yourown_function(predictor1=data.frame$predictor1, predictor2=data.frame$predictor2,....)
    

    using the new values, you can compute residuals, MSE, etc...

    0 讨论(0)
提交回复
热议问题