簡體   English   中英

循環數據幀列表中列的 lm 模型並輸出顯示斜率和 p 值的數據幀

[英]Looping lm models of column in a list of dataframes and outputting dataframes showing the slope and p values

我想在按因子拆分的數據幀列表中使用解釋變量循環變量i (響應)的lm()模型。 最后,我想創建兩個顯示lm系數的數據框:第一個顯示slope ,第二個顯示p.value ,其中在模型中測試的響應變量作為 cols 和行中的因子水平。

我設法運行並打印了lm模型summary的 output,但不確定如何創建適當的slopep.value數據幀。

這是我所做的:

data (iris)
iris_split = split (iris,f=iris$Species) ### Split the data by factor "Species"

我想用Petal.Width為以下每個變量運行 lm 模型(出於問題的考慮被視為響應)

vars = as.vector (unique (colnames (subset (iris, select = -c(Species, Petal.Width )))))
#Output:
#> vars
#[1] "Sepal.Length" "Sepal.Width"  "Petal.Length"
iris_lm = for (i in vars) { # loop across vars
  lm_summary = lapply (iris_split, FUN = function(x) 
                summary(lm (x[,i] ~ x[,"Petal.Width"]))) #Where (x) is levels of factors "Species"
                print(i) # so I could see which variable is tested in the model
                print(lm_summary)
}

如何創建slop.dfp.val.df 他們需要看起來像這樣:

#> slop.df
#     Species Sepal.Length Sepal.Width Petal.Length
#1     setosa       slope?      slope?       slope?
#2 versicolor       slope?      slope?       slope?
#3  virginica       slope?      slope?       slope?

需要顯示實際斜率而不是"slope?" 占位符,同樣適用於p.val.df

來自 [tidyverse][1] 的包使這相當方便:

iris %>% 
    pivot_longer(-c(Species, Petal.Width),
                 names_to = 'variable',
                 values_to = 'value'
                 ) %>% 
    group_by(Species, variable) %>% 
    ## mind to return the model results as a list!
    summarise(model_summary = list(summary(lm(Petal.Width ~ value)))) %>% 
    rowwise %>%
    mutate(slope = model_summary$coefficients[2, 'Estimate'],
           ## p = model_summary$coefficients[2, 'Pr(>|t|)']
           ) %>%
    ungroup %>%
    pivot_wider(id_cols = Species,
                names_from = 'variable',
                values_from = 'slope')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM