如何提取R中回归曲线的值?
How to extract values of regression curve in R?
如何在 R 中提取回归曲线的 x 值和 y 值?
生成一些示例数据并合并到数据框中:
x <- c(54, 54, 54, 54, 54, 54, 54, 72, 72, 72, 90, 90, 90, 90, 90, 90, 90, 90, 90, 72, 72, 72, 72, 72, 54, 54, 54, 54, 54, 54, 54, 72, 90, 90, 90, 90, 90, 90, 90, 90, 108, 126, 144, 144, 144, 144)
y <- c(15, 15, 15, 7.50, 7.50, 7.50, 25, 15, 15, 7.50, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 15, 0, 25, 15, 7.50, 7.50, 0, 7.50, 7.50, 7.50, 7.50, 7.50, 0, 3, 3, 3, 3, 15, 3, 15, 3, 3, 7.50)
df_xy <- as.data.frame(cbind(x, y))
df_xy
可视化数据:
plot(df_xy[,2], df_xy[,1])
回归分析(代码来自here)
生成训练和测试数据:
library(tidyverse)
library(caret)
set.seed(123)
training.samples <- df_xy[,1] %>%
createDataPartition(p = 0.8, list = FALSE)
train.data <- df_xy[training.samples, ]
test.data <- df_xy[-training.samples, ]
应用样条回归:
library(splines)
knots <- quantile(train.data$y, p = c(0.25, 0.5, 0.75))
model <- lm (x ~ bs(y, knots = knots), data = train.data)
可视化:
ggplot(train.data, aes(y, x) ) +
geom_point() +
stat_smooth(method = lm, formula = y ~ splines::bs(x, df =3))
有没有办法提取该曲线(蓝线)的 x 值和相应的 y 值?
此外,可能有一个基本的 R 选项来可视化样条回归吗?
补充问题:
此显示中显示了哪些 uncertainty/error 条柱?以及如何在各自的代码中指定?
如何提取不确定带(灰色区域)的 x 值和各自的 y 值?
如果将绘图保存为 gg1
,则
ggplot_build(gg1)$data[[2]][,c("x","y")]
将为您提供曲线的 x 和 y 坐标。
如何在 R 中提取回归曲线的 x 值和 y 值?
生成一些示例数据并合并到数据框中:
x <- c(54, 54, 54, 54, 54, 54, 54, 72, 72, 72, 90, 90, 90, 90, 90, 90, 90, 90, 90, 72, 72, 72, 72, 72, 54, 54, 54, 54, 54, 54, 54, 72, 90, 90, 90, 90, 90, 90, 90, 90, 108, 126, 144, 144, 144, 144)
y <- c(15, 15, 15, 7.50, 7.50, 7.50, 25, 15, 15, 7.50, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 15, 0, 25, 15, 7.50, 7.50, 0, 7.50, 7.50, 7.50, 7.50, 7.50, 0, 3, 3, 3, 3, 15, 3, 15, 3, 3, 7.50)
df_xy <- as.data.frame(cbind(x, y))
df_xy
可视化数据:
plot(df_xy[,2], df_xy[,1])
回归分析(代码来自here)
生成训练和测试数据:
library(tidyverse)
library(caret)
set.seed(123)
training.samples <- df_xy[,1] %>%
createDataPartition(p = 0.8, list = FALSE)
train.data <- df_xy[training.samples, ]
test.data <- df_xy[-training.samples, ]
应用样条回归:
library(splines)
knots <- quantile(train.data$y, p = c(0.25, 0.5, 0.75))
model <- lm (x ~ bs(y, knots = knots), data = train.data)
可视化:
ggplot(train.data, aes(y, x) ) +
geom_point() +
stat_smooth(method = lm, formula = y ~ splines::bs(x, df =3))
有没有办法提取该曲线(蓝线)的 x 值和相应的 y 值? 此外,可能有一个基本的 R 选项来可视化样条回归吗?
补充问题:
此显示中显示了哪些 uncertainty/error 条柱?以及如何在各自的代码中指定?
如何提取不确定带(灰色区域)的 x 值和各自的 y 值?
如果将绘图保存为 gg1
,则
ggplot_build(gg1)$data[[2]][,c("x","y")]
将为您提供曲线的 x 和 y 坐标。