整洁的引导不起作用,tidy():UseMethod(“ tidy”)中的错误:没有适用于“ tidy”的适用方法应用于类“ nls”的对象

时间:2019-12-10 09:14:20

标签: r tidy broom

我对R很陌生... 我试图按照Hadley和其他人的建议在rsample包中使用bootstrap函数,但是它似乎不起作用。也许有一个错误?谁能帮助解决这个问题? 在“整理自举”的帮助窗口中提供了此示例。有很多错误

> ggplot(mtcars, aes(wt, mpg)) +
+   geom_point() +
+   geom_line(aes(y = predict(nlsfit)))
> 
> 
> ####Bootsrapping
> library(dplyr)
> library(rsample)
> library(broom)
> library(purrr)
> 
> 
> library(dplyr)
> set.seed(104)
> boots <- bootstraps(mtcars, times = 100)
> boots
# Bootstrap sampling 
# A tibble: 100 x 2
   splits          id          
   <list>          <chr>       
 1 <split [32/11]> Bootstrap001
 2 <split [32/12]> Bootstrap002
 3 <split [32/13]> Bootstrap003
 4 <split [32/10]> Bootstrap004
 5 <split [32/15]> Bootstrap005
 6 <split [32/9]>  Bootstrap006
 7 <split [32/10]> Bootstrap007
 8 <split [32/12]> Bootstrap008
 9 <split [32/10]> Bootstrap009
10 <split [32/14]> Bootstrap010
# … with 90 more rows
> 
> 
> ####Creating a helper function to model each bootstrap sample
> fit_nls_on_bootstrap <- function(split) {
+   nls(mpg ~ k / wt + b, analysis(split), start = list(k = 1, b = 0))
+ }
> 
> boot_models <- boots %>% 
+   mutate(model = map(splits, fit_nls_on_bootstrap),
+          coef_info = map(model, tidy))
Error in UseMethod("tidy") : 
  no applicable method for 'tidy' applied to an object of class "nls"
> 
> boot_coefs <- boot_models %>% 
+   unnest(coef_info)
Error: Can't slice a scalar
> 
> 
> 
> #####calculated conifdence intervals
> alpha <- .05
> boot_coefs %>% 
+   group_by(term) %>%
+   summarize(low = quantile(estimate, alpha / 2),
+             high = quantile(estimate, 1 - alpha / 2))
Error: Column `term` is unknown
> ```

0 个答案:

没有答案