lasso回歸非常常用,,默認(rèn)的圖不丑,,但是總有人想要自定義,想要更好看,。
其實(shí)畫圖很簡單,,難的是提取數(shù)據(jù)。??
說到提取數(shù)據(jù),,說難不難,,說簡單不簡單,如果你會(huì)用搜索,,就非常簡單,根本不用自己寫,,一般來說,,你遇到過的問題,有人肯定早就遇到過且解決了,!
下面就給大家演示下怎么用1行代碼提取數(shù)據(jù),!包你滿意!包治百??!
加載R包
默認(rèn)畫圖
提取數(shù)據(jù)
自定義繪圖
加載R包
library(glmnet)
## 載入需要的程輯包:Matrix
## Loaded glmnet 4.1-3
library(survival)
自帶的生存分析數(shù)據(jù),,方便學(xué)習(xí)。
data(CoxExample)
x <- CoxExample$x
y <- CoxExample$y
head(y)
## time status
## [1,] 1.76877757 1
## [2,] 0.54528404 1
## [3,] 0.04485918 0
## [4,] 0.85032298 0
## [5,] 0.61488426 1
## [6,] 0.29860939 0
默認(rèn)畫圖
mod <- glmnet(x,y,family = "cox")
cvmod <- cv.glmnet(x,y,family="cox") # 交叉驗(yàn)證
下面3個(gè)都是默認(rèn)的函數(shù)畫的圖,,真不能說丑,,你看超多SCI里都是直接用的原圖。
plot(mod,label = T,lwd=2)
plot(mod,xvar = "lambda",label = T,lwd=2)
plot(cvmod)
提取數(shù)據(jù)
如果你想提取數(shù)據(jù),,也沒什么難度,,就是慢慢找,肯定能找到,。
library(tidyverse)
## -- Attaching packages ----------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.1 v forcats 0.5.1
## -- Conflicts -------------------------------- tidyverse_conflicts() --
## x tidyr::expand() masks Matrix::expand()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x tidyr::pack() masks Matrix::pack()
## x tidyr::unpack() masks Matrix::unpack()
tmp <- as_tibble(as.matrix(coef(mod)), rownames = "coef") %>%
pivot_longer(cols = -coef,
names_to = "variable",
names_transform = list(variable = parse_number),
values_to = "value") %>%
group_by(variable) %>%
mutate(lambda = mod$lambda[variable + 1],
norm = sum(if_else(coef == "(Intercept)", 0, abs(value))))
接下來畫圖就非常簡單,。
ggplot(tmp, aes(norm,value,color=coef,group=coef))+
geom_line(size=1.2)+
labs(x="Log Lambda",y="Coefficients")+
theme_bw()
當(dāng)然,肯定也有簡便方法,!
俗話說得好:
“包治百病,,包你滿意。
下面就是我的表演,,用包搞定這個(gè)看似復(fù)雜的問題,!
這個(gè)包很神奇,大家有空可以學(xué)習(xí)下,,沒空的話等我更新,。??
# 就是這個(gè)包,沒裝的自己安裝下,!
library(broom)
# 提取數(shù)據(jù),,就是這么簡單!
tidy_df <- broom::tidy(mod)
tidy_cvdf <- broom::tidy(cvmod)
夠簡單嗎,?我覺得比自己擼代碼簡單,。
# 給大家看看數(shù)據(jù)結(jié)構(gòu)
head(tidy_df)
## # A tibble: 6 x 5
## term step estimate lambda dev.ratio
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 V1 3 0.0312 0.197 0.00596
## 2 V1 4 0.0637 0.179 0.0105
## 3 V1 5 0.0939 0.163 0.0144
## 4 V1 6 0.125 0.149 0.0188
## 5 V1 7 0.153 0.135 0.0226
## 6 V1 8 0.179 0.123 0.0256
head(tidy_cvdf)
## # A tibble: 6 x 6
## lambda estimate std.error conf.low conf.high nzero
## <dbl> <dbl> <dbl> <dbl> <dbl> <int>
## 1 0.237 13.7 0.0429 13.7 13.7 0
## 2 0.216 13.7 0.0427 13.6 13.7 1
## 3 0.197 13.6 0.0421 13.6 13.7 3
## 4 0.179 13.6 0.0419 13.5 13.6 3
## 5 0.163 13.5 0.0422 13.5 13.6 4
## 6 0.149 13.5 0.0429 13.4 13.5 4
自定義繪圖
有了數(shù)據(jù),畫圖就很簡單了,。
library(ggplot2)
library(RColorBrewer)
#隨便定義幾個(gè)顏色,,多找?guī)讉€(gè),防止不夠用
mypalette <- c(brewer.pal(11,"BrBG"),brewer.pal(11,"Spectral"),brewer.pal(5,"Accent"))
ggplot(tidy_df, aes(step, estimate, group = term,color=term)) +
geom_line(size=1.2)+
geom_hline(yintercept = 0)+
ylab("Coefficients")+
scale_color_manual(name="variable",values = mypalette)+
theme_bw()
p2 <- ggplot(tidy_df, aes(lambda, estimate, group = term, color = term)) +
geom_line(size=1.2)+
geom_hline(yintercept = 0)+
scale_x_log10(name = "Log Lambda")+
ylab("Coefficients")+
scale_color_manual(name="variable",values = mypalette)+
theme_bw()
p2
p3 <- ggplot()+
geom_point(data=tidy_cvdf, aes(lambda,estimate))+
geom_errorbar(data = tidy_cvdf, aes(x=lambda,ymin=conf.low,ymax=conf.high))+
scale_x_log10(name = "Log Lambda")+
ylab("Coefficients")+
theme_bw()
p3
如果你發(fā)文章用的話,,橫坐標(biāo)多數(shù)都是log lambda,,所以你可以把第2張和第3張拼一起,完全不影響使用,。
library(patchwork)
p2 / p3