我有以下数据集,我试图用 ggplot2 绘制,它是三个实验 A1、B1 和 C1 的时间序列,每个实验都有三个重复。
我正在尝试添加一个统计信息,该统计信息在返回更平滑(均值和方差?)之前检测并删除异常值。我已经编写了自己的异常值函数(未显示),但我希望已经有一个函数可以做到这一点,我只是还没有找到它。
我从 ggplot2 书中的一些示例中查看了 stat_sum_df("median_hilow", geom = "smooth") ,但我不理解 Hmisc 的帮助文档,看看它是否删除了异常值。
在ggplot中是否有删除此类异常值的功能,或者我将在哪里修改下面的代码以添加我自己的功能?
编辑:我刚刚看到这个(如何在 R 代码中使用异常值测试)并注意到 Hadley 建议使用稳健的方法,例如 rlm。我正在绘制细菌生长曲线,所以我认为线性模型不是最好的,但对于其他模型或在这种情况下使用或使用稳健模型的任何建议将不胜感激。
library (ggplot2)
data = data.frame (day = c(1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7,1,3,5,7), od =
c(
0.1,1.0,0.5,0.7
,0.13,0.33,0.54,0.76
,0.1,0.35,0.54,0.73
,1.3,1.5,1.75,1.7
,1.3,1.3,1.0,1.6
,1.7,1.6,1.75,1.7
,2.1,2.3,2.5,2.7
,2.5,2.6,2.6,2.8
,2.3,2.5,2.8,3.8),
series_id = c(
"A1", "A1", "A1","A1",
"A1", "A1", "A1","A1",
"A1", "A1", "A1","A1",
"B1", "B1","B1", "B1",
"B1", "B1","B1", "B1",
"B1", "B1","B1", "B1",
"C1","C1", "C1", "C1",
"C1","C1", "C1", "C1",
"C1","C1", "C1", "C1"),
replicate = c(
"A1.1","A1.1","A1.1","A1.1",
"A1.2","A1.2","A1.2","A1.2",
"A1.3","A1.3","A1.3","A1.3",
"B1.1","B1.1","B1.1","B1.1",
"B1.2","B1.2","B1.2","B1.2",
"B1.3","B1.3","B1.3","B1.3",
"C1.1","C1.1","C1.1","C1.1",
"C1.2","C1.2","C1.2","C1.2",
"C1.3","C1.3","C1.3","C1.3"))
> data
day od series_id replicate
1 1 0.10 A1 A1.1
2 3 1.00 A1 A1.1
3 5 0.50 A1 A1.1
4 7 0.70 A1 A1.1
5 1 0.13 A1 A1.2
6 3 0.33 A1 A1.2
7 5 0.54 A1 A1.2
8 7 0.76 A1 A1.2
9 1 0.10 A1 A1.3
10 3 0.35 A1 A1.3
11 5 0.54 A1 A1.3
12 7 0.73 A1 A1.3
13 1 1.30 B1 B1.1
... etc...
这是我到目前为止所拥有的并且运行良好,但异常值没有被删除:
r <- ggplot(data = data, aes(x = day, y = od))
r + geom_point(aes(group = replicate, color = series_id)) + # add points
geom_line(aes(group = replicate, color = series_id)) + # add lines
geom_smooth(aes(group = series_id)) # add smoother, average of each replicate
编辑:我刚刚在下面添加了两个图表,显示了我从真实数据而不是上面的示例数据中遇到的异常问题的示例。
第一个图显示了 p26s4 系列,在第 32 天左右,两个复制品发生了非常奇怪的事情,显示了 2 个异常值。
第二个图显示了 p22s5 系列,在第 18 天,那天的读数发生了一些奇怪的事情,我认为可能是机器错误。
目前我正在观察数据,以检查增长曲线是否正常。在采纳 Hadley 的建议并设置 family =“对称”之后,我相信黄土平滑器在忽略异常值方面做得不错。
@Peter/@hadley,接下来我想做的是尝试将逻辑、gompertz 或 Richard 的增长曲线拟合到这些数据而不是黄土,并计算指数阶段的增长率。最终我计划在 R 中使用 grofit 包(http://cran.r-project.org/web/packages/grofit/index.html),但现在我想尽可能使用 ggplot2 手动绘制这些。如果您有任何指示,将不胜感激。