如何在R中对特定数据集使用predict并在ggplot2中绘制?
我有如下数据:如何在R中对特定数据集使用predict并在ggplot2中绘制?,r,ggplot2,predict,R,Ggplot2,Predict,我有如下数据: 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-6
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
然后,我想使用拟合的黄土曲线进行预测,如下所示:
# 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)
#定义模型
你的意思是这样的吗
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")
p是的,这真的很有帮助。我只是想知道ggplot2中是否有我丢失的内置函数?对于预测和预测绘图而言,黄土是一种更平滑的材料,实际上并不用于预测目的。您应该查看非线性或广义线性模型,而不是报告预测。
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")