代码之家  ›  专栏  ›  技术社区  ›  MYaseen208

对新数据使用预测函数和潮差

  •  0
  • MYaseen208  · 技术社区  · 6 年前

    predict 用于新数据的函数以及 tidyverse 如下例所示。但是,我不知道如何使用wt=4.0和4.2的新数据。有什么提示吗。

    library(tidyverse)    
      mtcars %>%
        dplyr::mutate(cyl1 = factor(cyl)) %>%
        tidyr::nest(-cyl) %>%
        dplyr::mutate(m1  = purrr::map(.x = data, .f = ~ lm(mpg ~ wt, data = .))) %>%
        mutate(Pred = purrr::map(.x = m1, .f = predict)) %>%
        dplyr::pull(Pred)
    
    1 回复  |  直到 6 年前
        1
  •  4
  •   missuse    6 年前

    下面是一个关于如何按组拟合多个模型并从中获得预测的示例。

    newdat <- data.frame(wt = c(4,4.2))
    library(tidyverse)  
    mtcars %>%
      group_by(cyl) %>% #group by cyl
      nest %>% #nest groups
      mutate(m1  = purrr::map(.x = data, .f = ~ lm(mpg ~ wt, data = .))) %>% #create models
      mutate(Pred = purrr::map(.x = m1, ~ predict(., newdat))) %>% #predict on new data
      pull(Pred) #pull predictions
    #output
    [[1]]
           1        2 
    17.28842 16.73240 
    
    [[2]]
           1        2 
    16.98309 15.85369 
    
    [[3]]
           1        2 
    15.09828 14.65979 
    

    或稍加修改:

    mtcars %>%
      group_by(cyl) %>%
      nest %>%
      mutate(m1  = purrr::map(.x = data, .f = ~ lm(mpg ~ wt, data = .))) %>%
      mutate(Pred = purrr::map(.x = m1, ~ predict(., newdat))) %>%
      select(cyl, Pred) %>%
      unnest #one can add %>% cbind(newdat = newdat) to know for which wt the pred is for
    
    #output
    # A tibble: 6 x 2
        cyl  Pred
      <dbl> <dbl>
    1  6.00  17.3
    2  6.00  16.7
    3  4.00  17.0
    4  4.00  15.9
    5  8.00  15.1
    6  8.00  14.7
    

    编辑评论中的问题

    为了获得标准误差,我认为最简单的方法是为predict定义一个自定义函数,该函数将返回 fit se.fit :

    pred <- function(x,  ...){
      z <- predict.lm(x, se.fit = TRUE, ...)
      as.data.frame(z[1:2])
    }
    
    mtcars %>%
      mutate(cyl1 = factor(cyl)) %>%
      group_by(cyl) %>%
      nest %>%
      mutate(m1  = purrr::map(.x = data, .f = ~ lm(mpg ~ wt, data = .))) %>%
      mutate(Pred = purrr::map(.x = m1, ~ pred(., newdat = newdat))) %>%
      select(cyl, Pred) %>%
      unnest %>%
      cbind(newdat = newdat)
    #output
      cyl      fit    se.fit  wt
    1   6 17.28842 1.2581400 4.0
    2   6 16.73240 1.5111249 4.2
    3   4 16.98309 3.3269446 4.0
    4   4 15.85369 3.6813880 4.2
    5   8 15.09828 0.5409614 4.0
    6   8 14.65979 0.5609545 4.2