Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用tidyverse在r中进行蒙特卡罗模拟_R_Simulation - Fatal编程技术网

用tidyverse在r中进行蒙特卡罗模拟

用tidyverse在r中进行蒙特卡罗模拟,r,simulation,R,Simulation,以下是我的数据: df1一个很酷的方法是使用推断包 library(tidyverse) library(infer) df1 %>% specify(y ~ x) %>% generate(reps = 100, type = "bootstrap") %>% calculate(stat = "correlation") %>% summarise(odds = stat %>% mean(),sd = stat %>% sd) d

以下是我的数据:


df1一个很酷的方法是使用推断包

library(tidyverse)
library(infer)

 df1 %>%
  specify(y ~ x) %>%
  generate(reps = 100, type = "bootstrap") %>%
  calculate(stat = "correlation") %>% 
  summarise(odds = stat %>% mean(),sd = stat %>% sd)

df1 %>%
  specify(y ~ x) %>%
  generate(reps = 100, type = "bootstrap") %>%
  calculate(stat = "slope") %>% 
  summarise(beta = stat %>% mean,sd = stat %>% sd)

这是另一个不太清楚的答案

library(tidymodels)
set.seed(42)
bootstrap_data <- df1 %>% 
  rsample::bootstraps(100)

fit_lm_on_bootstrap <- function(split) {
  lm(y ~ x,data= split)
}


boot_models <- bootstrap_data %>% 
  mutate(model = map(.x = splits,fit_lm_on_bootstrap),
         tidy_results = map(model,tidy)) %>% 
  unnest(tidy_results)

boot_models %>%
  filter(term == "(Intercept)") %>% 
  summarise_at(vars(estimate:p.value),mean)

# A tibble: 1 x 4
  estimate std.error statistic p.value
     <dbl>     <dbl>     <dbl>   <dbl>
1     4.07      3.77      1.23   0.298

boot_models %>%
  filter(term == "x") %>% 
  summarise_at(vars(estimate:p.value),mean)

# A tibble: 1 x 4
  estimate std.error statistic     p.value
     <dbl>     <dbl>     <dbl>       <dbl>
1     10.4      1.16      9.25 0.000000136

通过这样做,您试图从数据中学到什么,这看起来有点像自举。你说做得更好是什么意思?更快,更正确?您对tidyverse代码的限制有多大,因为better通常意味着对于better的某些值,更多的基本R代码和更少的tidyverse?也许您可以详细说明您试图通过Monte Carlo模拟实现的目标。蒙特卡罗模拟本身是一种技术,而不是一种分析方法。我试图展示一些平均结果,但正如其他人所说,蒙特卡罗只是一种技术,你想用它来分析什么完全取决于你,我想如果你想分析某种差异,你可以计算统计列的sd,我不知道,我想这个软件包没有一个简单的方法,我想你不需要截距,毕竟是的,x是斜率,它很棒now@User20100如果它解决了你的问题,请考虑投票和检查问题。回答:你可以把answes和bind_row结合起来
library(tidymodels)
set.seed(42)
bootstrap_data <- df1 %>% 
  rsample::bootstraps(100)

fit_lm_on_bootstrap <- function(split) {
  lm(y ~ x,data= split)
}


boot_models <- bootstrap_data %>% 
  mutate(model = map(.x = splits,fit_lm_on_bootstrap),
         tidy_results = map(model,tidy)) %>% 
  unnest(tidy_results)

boot_models %>%
  filter(term == "(Intercept)") %>% 
  summarise_at(vars(estimate:p.value),mean)

# A tibble: 1 x 4
  estimate std.error statistic p.value
     <dbl>     <dbl>     <dbl>   <dbl>
1     4.07      3.77      1.23   0.298

boot_models %>%
  filter(term == "x") %>% 
  summarise_at(vars(estimate:p.value),mean)

# A tibble: 1 x 4
  estimate std.error statistic     p.value
     <dbl>     <dbl>     <dbl>       <dbl>
1     10.4      1.16      9.25 0.000000136