如何使用 r.c 绘制 ols。样条
How to plot ols with r.c. splines
我想绘制回归的预测线,由于模型和标准误差带中的非线性,该线包含受限的三次样条。我可以获得预测点,但不确定是否只绘制线条和误差带。 ggplot 是首选,或者基本图形也可以。谢谢
这是文档中的示例:
library(rms)
# Fit a complex model and approximate it with a simple one
x1 <- runif(200)
x2 <- runif(200)
x3 <- runif(200)
x4 <- runif(200)
y <- x1 + x2 + rnorm(200)
f <- ols(y ~ rcs(x1,4) + x2 + x3 + x4)
pred <- fitted(f) # or predict(f) or f$linear.predictors
f2 <- ols(pred ~ rcs(x1,4) + x2 + x3 + x4, sigma=1)
fastbw(f2, aics=100000)
options(datadist=NULL)
以及模型预测值图:
plot(predict(f2))
rms
包有许多用于此目的的有用函数。值得一看http://biostat.mc.vanderbilt.edu/wiki/Main/RmS
在这种情况下,您可以适当地简单设置 datadist
(为预测变量设置分布摘要),然后使用 plot(Predict(f)
或 ggplot(Predict(f))
set.seed(5)
# Fit a complex model and approximate it with a simple one
x1 <- runif(200)
x2 <- runif(200)
x3 <- runif(200)
x4 <- runif(200)
y <- x1 + x2 + rnorm(200)
f <- ols(y ~ rcs(x1,4) + x2 + x3 + x4)
ddist <- datadist(x1,x2,x3,x4)
options(datadist='ddist')
plot(Predict(f))
ggplot(Predict(f))
我想绘制回归的预测线,由于模型和标准误差带中的非线性,该线包含受限的三次样条。我可以获得预测点,但不确定是否只绘制线条和误差带。 ggplot 是首选,或者基本图形也可以。谢谢
这是文档中的示例:
library(rms)
# Fit a complex model and approximate it with a simple one
x1 <- runif(200)
x2 <- runif(200)
x3 <- runif(200)
x4 <- runif(200)
y <- x1 + x2 + rnorm(200)
f <- ols(y ~ rcs(x1,4) + x2 + x3 + x4)
pred <- fitted(f) # or predict(f) or f$linear.predictors
f2 <- ols(pred ~ rcs(x1,4) + x2 + x3 + x4, sigma=1)
fastbw(f2, aics=100000)
options(datadist=NULL)
以及模型预测值图:
plot(predict(f2))
rms
包有许多用于此目的的有用函数。值得一看http://biostat.mc.vanderbilt.edu/wiki/Main/RmS
在这种情况下,您可以适当地简单设置 datadist
(为预测变量设置分布摘要),然后使用 plot(Predict(f)
或 ggplot(Predict(f))
set.seed(5)
# Fit a complex model and approximate it with a simple one
x1 <- runif(200)
x2 <- runif(200)
x3 <- runif(200)
x4 <- runif(200)
y <- x1 + x2 + rnorm(200)
f <- ols(y ~ rcs(x1,4) + x2 + x3 + x4)
ddist <- datadist(x1,x2,x3,x4)
options(datadist='ddist')
plot(Predict(f))
ggplot(Predict(f))