如何在特定数据集上使用 R 中的预测并将其绘制在 ggplot2 中?
How to use predict in R on specific set of data and plot it in ggplot2?
我有这样的数据:
time level strain
<dbl> <dbl> <chr>
1 0.0 0.000 M12-611020
2 1.0 0.088 M12-611020
3 3.0 0.211 M12-611020
4 4.0 0.278 M12-611020
5 4.5 0.404 M12-611020
6 5.0 0.606 M12-611020
7 5.5 0.778 M12-611020
8 6.0 0.902 M12-611020
9 6.5 1.024 M12-611020
10 8.0 1.100 M12-611020
11 0.0 0.000 M12-611025
12 1.0 0.077 M12-611025
13 3.0 0.088 M12-611025
14 4.0 0.125 M12-611025
15 5.0 0.304 M12-611025
16 5.5 0.421 M12-611025
17 6.0 0.518 M12-611025
18 6.5 0.616 M12-611025
19 7.0 0.718 M12-611025
我可以使用 ggplot 轻松绘制它,让 ggplot 单独查看应变并使用 stat_smooth 拟合曲线:
ggplot(data = data, aes(x = time, y = level), group = strain) + stat_smooth(aes(group=strain,fill=strain, colour = strain) ,method = "loess", se = F, span = 0.8) +
theme_gray()+xlab("Time(h)") +
geom_point(aes(fill=factor(strain)),alpha=0.5 , size=3,shape = 21,colour = "black", stroke = 1)+
theme(legend.position="right")
然后我想使用适合我的黄土曲线进行预测,如下所示:
# define the model
model <- loess(time ~ strain,span = 0.8, data = data)
# Predict for given levle (x) the time (y)
predict(model, newdata = 0.3, se = FALSE)
但是我不知道如何预测我上面列出的 "strains" 中的一个或另一个(即图中的红线或蓝线)?
此外,是否有一种简单的方法可以在图表上以虚线的形式在图表上绘制此预测,如上所示,虚线从 0.3 下降到预测时间?
你的意思是这样的吗?
p <- ggplot(data = dat, aes(x = time, y = level, fill = strain)) +
geom_point(alpha=0.5 , size=3,shape = 21, colour = "black", stroke = 1) +
stat_smooth(aes(group=strain, colour=strain) ,method = "loess", se = F, span = 0.8)
newdat <- split(dat, dat$strain)
mod <- lapply(newdat, function(x)loess(level ~ time,span = 0.8, data = x))
predict(mod[["M12-611020"]], newdata = 2, se = FALSE)
p +
geom_segment(aes(x=2, xend=2, y=0, yend=0.097), linetype="dashed") +
geom_segment(aes(x=0, xend=2, y=0.097, yend=0.097), linetype="dashed")
我有这样的数据:
time level strain
<dbl> <dbl> <chr>
1 0.0 0.000 M12-611020
2 1.0 0.088 M12-611020
3 3.0 0.211 M12-611020
4 4.0 0.278 M12-611020
5 4.5 0.404 M12-611020
6 5.0 0.606 M12-611020
7 5.5 0.778 M12-611020
8 6.0 0.902 M12-611020
9 6.5 1.024 M12-611020
10 8.0 1.100 M12-611020
11 0.0 0.000 M12-611025
12 1.0 0.077 M12-611025
13 3.0 0.088 M12-611025
14 4.0 0.125 M12-611025
15 5.0 0.304 M12-611025
16 5.5 0.421 M12-611025
17 6.0 0.518 M12-611025
18 6.5 0.616 M12-611025
19 7.0 0.718 M12-611025
我可以使用 ggplot 轻松绘制它,让 ggplot 单独查看应变并使用 stat_smooth 拟合曲线:
ggplot(data = data, aes(x = time, y = level), group = strain) + stat_smooth(aes(group=strain,fill=strain, colour = strain) ,method = "loess", se = F, span = 0.8) +
theme_gray()+xlab("Time(h)") +
geom_point(aes(fill=factor(strain)),alpha=0.5 , size=3,shape = 21,colour = "black", stroke = 1)+
theme(legend.position="right")
然后我想使用适合我的黄土曲线进行预测,如下所示:
# define the model
model <- loess(time ~ strain,span = 0.8, data = data)
# Predict for given levle (x) the time (y)
predict(model, newdata = 0.3, se = FALSE)
但是我不知道如何预测我上面列出的 "strains" 中的一个或另一个(即图中的红线或蓝线)?
此外,是否有一种简单的方法可以在图表上以虚线的形式在图表上绘制此预测,如上所示,虚线从 0.3 下降到预测时间?
你的意思是这样的吗?
p <- ggplot(data = dat, aes(x = time, y = level, fill = strain)) +
geom_point(alpha=0.5 , size=3,shape = 21, colour = "black", stroke = 1) +
stat_smooth(aes(group=strain, colour=strain) ,method = "loess", se = F, span = 0.8)
newdat <- split(dat, dat$strain)
mod <- lapply(newdat, function(x)loess(level ~ time,span = 0.8, data = x))
predict(mod[["M12-611020"]], newdata = 2, se = FALSE)
p +
geom_segment(aes(x=2, xend=2, y=0, yend=0.097), linetype="dashed") +
geom_segment(aes(x=0, xend=2, y=0.097, yend=0.097), linetype="dashed")