![](/img/trans.png)
[英]Error when using calibrate function for ols model fitted in rms package
[英]Contour plot using bplot function in rms package
我一直試圖基於“ rms”包中的bplot函數,為帶有R的預測模型繪制輪廓圖。 代碼如下:
library(rms)
n <- 1000
set.seed(17)
age <- rnorm(n, 50, 10)
blood.pressure <- rnorm(n, 120, 15)
cholesterol <- rnorm(n, 200, 25)
sex <- factor(sample(c('female','male'), n,TRUE))
L <- .4*(sex=='male') + .045*(age-50) + (log(cholesterol - 10)-5.2)*(-2*(sex=='female') + 2*(sex=='male'))
y <- ifelse(runif(n) < plogis(L), 1, 0)
ddist <- datadist(age, blood.pressure, cholesterol, sex)
options(datadist='ddist')
fit <- lrm(y ~ blood.pressure + sex * (age + rcs(cholesterol,4)), x=TRUE, y=TRUE)
p <- Predict(fit, age, cholesterol, sex, np=50)
bplot(p,, contourplot, region = TRUE,col.regions=topo.colors)
我注意到輸出圖是這樣的:
我找不到如何平滑兩個填充區域之間的鋸齒形邊界線的方法,所以我想知道是否可以使用ggplot2制作這種用於預測模型的等高線圖,或者是否有其他解決方案來平滑鋸齒形邊界線。
您可以結合使用geom_tile
和geom_contour
繪制相似的圖。
library(ggplot2)
ggplot(data.frame(p), aes(age, cholesterol, fill = yhat, z = yhat)) +
geom_tile() +
geom_contour(color = "black") +
scale_fill_distiller(palette = "Spectral", limits = c(-2, 2)) +
labs(x = "Age",
y = expression(Total~Cholesterol["mg/dl"]),
fill = NULL) +
facet_grid(~ sex) +
theme_classic()
編輯:根據OP的要求,我添加了離散的顏色:
ggplot(data.frame(p), aes(age, cholesterol, z = yhat)) +
geom_tile(aes(fill = factor(round(yhat)))) +
geom_contour(color = "black") +
labs(x = "Age",
y = expression(Total~Cholesterol["mg/dl"]),
fill = NULL) +
facet_grid(~ sex) +
theme_classic()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.