Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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
如何忽略cor.test:“;有限观测值不足”;并在使用tidyverse和ggplot2(ggpmisc)时继续_R_Ggplot2_Correlation_Tidyr - Fatal编程技术网

如何忽略cor.test:“;有限观测值不足”;并在使用tidyverse和ggplot2(ggpmisc)时继续

如何忽略cor.test:“;有限观测值不足”;并在使用tidyverse和ggplot2(ggpmisc)时继续,r,ggplot2,correlation,tidyr,R,Ggplot2,Correlation,Tidyr,我有以下工作玩具示例: trunctiris <- iris [1:102,] analysis <- trunctiris %>% group_by(Species) %>% nest() %>% mutate(model = map(data, ~lm(Sepal.Length ~ Sepal.Width, data = .)), cor = map(data, ~tidy(cor.test(.x$Sepal.Length, .x

我有以下工作玩具示例:

trunctiris <- iris [1:102,] 
analysis <- trunctiris %>%
  group_by(Species) %>%
  nest() %>%
  mutate(model = map(data, ~lm(Sepal.Length ~ Sepal.Width, data = .)),
         cor = map(data, ~tidy(cor.test(.x$Sepal.Length, .x$Sepal.Width), 3)))

stats <- analysis %>%
  unnest(cor)

ggplot(trunctiris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(shape = 21) +
  geom_text(data = stats, aes(label = sprintf("r = %s", round(estimate, 3)), x = 7, y = 4)) +
  geom_text(data = stats, aes(label = sprintf("p = %s", round(p.value, 3)),  x = 7, y = 3.8)) +
  geom_smooth(method = "lm", formula = y ~ x) +
  stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~~")),
               formula = y ~ x,
               parse = TRUE) +
  facet_wrap(~Species)
trunctiris%
嵌套()%>%
突变(模型=图谱(数据,~lm(萼片长度~萼片宽度,数据=),
cor=map(数据,~tidy(cor.test(.x$Sepal.Length,.x$Sepal.Width),3)))
统计数据%
不耐烦(cor)
ggplot(茎干,aes(x=萼片长度,y=萼片宽度))+
几何点(形状=21)+
geom_文本(数据=统计数据,aes(标签=sprintf(“r=%s”,四舍五入(估计值,3)),x=7,y=4))+
geom_文本(数据=统计数据,aes(标签=sprintf(“p=%s”,四舍五入(p.value,3)),x=7,y=3.8))+
几何光滑(方法=“lm”,公式=y~x)+
统计多边形均衡器(aes(标签=粘贴(…均衡器标签…,…rr.标签…,sep=“~~~”)),
公式=y~x,
parse=TRUE)+
面_包装(~种)
代码是在另一个问题中提供的。然而,我无法使它与我的数据一起工作。问题是,我有一些(并非所有)组的观察值少于3,因此,在“分析”部分R返回:

mutate_impl(.data,dots)中出错:有限的观测值不足

这与团队中没有足够的观察有关(在本例中为弗吉尼亚州)。我想绕过这个问题,我尝试过“try(如果nrow(data)>=2)”或类似的方法。。例如:

analysis <- iris %>% 
group_by(Species) %>% 
nest() %>% mutate(model = map(data, ~lm (Sepal.Length ~ Sepal.Width, data = .)), 
    cor = if_else( nrow(data) <= 2 , warning ("Must have at least 3 rows of data"), 
        (map(data, ~tidy(cor.test(.x$Sepal.Length, .x$Sepal.Width), 3)))))
分析%
组别(种类)%>%
nest()%>%突变(model=map(数据,~lm(Sepal.Length~Sepal.Width,数据=),

cor=if_else(nrow(data)
purr::safety
purr::mably
允许在
map
ping时轻松防范错误。在这种情况下,一个好的策略是在
mably
中封装对
tidy(cor.test)(…
的调用,并在发生错误时返回一个空的data.frame

library(purrr)
analysis <- trunctiris %>%
  group_by(Species) %>%
  nest() %>%
  mutate(
    model = map(data, ~lm(Sepal.Length ~ Sepal.Width, data = .)),
    cor = map(data, possibly(
      ~tidy(cor.test(.x$Sepal.Length, .x$Sepal.Width), 3), otherwise = data.frame())
    )
  )
#一个tible:2×9
物种估计统计p值参数形态低形态高
1 setosa 0.7425467.6807386.709843e-1048 0.5851391 0.8460314
2花色0.5259107 4.283887 8.771860e-05 48 0.2900175 0.7015599
#…还有两个变量:方法、备选方案

因此,给出错误的组被成功地从最终结果中删除。

我能想到的最简单的解决方案是在数据中预先检查,删除不符合此标准的组,并将结果data.frame提供给
ggplot
# A tibble: 3 × 4
     Species              data    model                  cor
      <fctr>            <list>   <list>               <list>
1     setosa <tibble [50 × 4]> <S3: lm> <data.frame [1 × 8]>
2 versicolor <tibble [50 × 4]> <S3: lm> <data.frame [1 × 8]>
3  virginica  <tibble [2 × 4]> <S3: lm> <data.frame [0 × 0]> #<- Note the empty df here
unnest(analysis)
# A tibble: 2 × 9
     Species  estimate statistic      p.value parameter  conf.low conf.high
      <fctr>     <dbl>     <dbl>        <dbl>     <int>     <dbl>     <dbl>
1     setosa 0.7425467  7.680738 6.709843e-10        48 0.5851391 0.8460314
2 versicolor 0.5259107  4.283887 8.771860e-05        48 0.2900175 0.7015599
# ... with 2 more variables: method <fctr>, alternative <fctr>