我想知道是否有更好的方法是將線性回歸系數作為 dplyr 中的列。這是一些示例資料。
mydata <-
data.frame(
Site = c(1,1,1,1,1,1,1,1),
Site1 = c(2,3,2,3,2,3,2,3),
Age = c(17, 52, 19, 18, 62, 53, 41, 24),
Gender = c(1,2,1,1,2,2,2,1),
Outcome = c(1,1,1,1,0,0,0,1)
)
我寫了這個輔助函式來summary(.data)$coefficients變成列
GetCoefficients <- function(.data){
AllData <- data.frame()
AllData[1, ] <- ""
col_names <- colnames(summary(.data)$coefficients)
row_names <- rownames(summary(.data)$coefficients)
row_len <- length(row_names)
col_len <- length(col_names)-1
x <- summary(.data)$coefficients
for (i in 1:length(x)){
AllData <- AllData %>%
mutate(!!paste0(row_names[ifelse(i%%row_len != 0, i%%row_len, row_len)],
"_",col_names[ceiling(i/col_len)]) := x[i])
}
return(AllData)
}
使用輔助函式,我可以將系數放入我的 data.frame()
Linear_regression <- mydata %>%
pivot_longer(starts_with("Site"),
names_to = ".value",
names_pattern = "(^Site)") %>%
group_by(Site) %>%
do(Reg = lm(Outcome ~ Age Gender, data = .)) %>%
mutate(rsq = summary(Reg)$r.squared) %>%
mutate(fun = GetCoefficients(Reg))
uj5u.com熱心網友回復:
這是一個組合tidyverse和broom包,以獲得您想要的輸出。
這里非常方便group_split-> 你得到一個串列,然后你用purrrs迭代map_dfr(順便說一句,map_dfr你得到一個資料框,否則map你得到一個串列)你的回歸lm(...通過每個串列元素。使用brooms Glance 可以得到所需的輸出:
library(tidyverse)
library(broom)
mydata %>%
pivot_longer(starts_with("Site"),
names_to = ".value",
names_pattern = "(^Site)") %>%
mutate(Site=as.factor(Site)) %>%
group_by(Site) %>%
group_split() %>%
map_dfr(.f = function(df){
lm(Outcome ~ Age Gender, data=df) %>%
glance() %>%
add_column(Site = unique(df$Site), .before = 1)
})
Site r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance df.residual nobs
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int>
1 1 0.6 0.44 3.87e- 1 3.75e 0 1.01e- 1 2 -1.88 11.8 12.1 7.5 e- 1 5 8
2 2 1 1 2.22e-16 1.01e 31 2.22e-16 2 141. -275. -277. 4.93e-32 1 4
3 3 0.351 -0.946 6.97e- 1 2.71e- 1 8.05e- 1 2 -1.46 10.9 8.47 4.86e- 1 1 4
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/409152.html
標籤:
上一篇:以特定方式替換陣列中的字母
