R - 将函数应用于 lm 摘要列表
R - Apply function to list of lm summaries
我有一个列表,其中包含不同线性回归的摘要。我想提取线性回归所有元素的系数和 p 值,并将它们放入一个数据帧中(每个 lm 元素一个数据帧)。
例如,要提取截距的系数,我使用以下方法:
func_coef_intercept <- function(x) list[[x]][["coefficients"]][1,1]
coef_intercept <- lapply(list, func_coef_intercept)
要提取 p 值:
func_coef_intercept <- function(x) list[[x]][["coefficients"]][1,4]
pvalue_intercept <- lapply(list, func_coef_intercept)
但是我遇到了这个错误
Error in list[[x]] : invalid subscript type 'list'
如何提取线性回归的所有元素的系数?
这是列表前两个摘要的示例:
list(`1` = structure(list(call = lm(formula = a ~ b * c + d),
terms = a ~ b * c + d, residuals = structure(c(`1002` = 0.0118681256952383,
`1007` = 0.0419256575878256, `1008` = -0.0963910775101885,
`1011` = 0.22304807403332, `1012` = -0.167863579846024, `1013` = -0.0613889863570078,
`1014` = 0.0357270798843175, `1015` = -0.172203913653826,
`1018` = -0.156689137194, `1019` = -0.129444770996805, `1021` = -0.175019531525519,
`1023` = 0.146466383569852, `1025` = 0.110813999746634, `1031` = -0.0920873415380558,
`1033` = 0.000742586091925349, `1034` = 0.181961137021771,
`1035` = 0.146326238597575, `1036` = -0.118020186381677,
`1037` = -0.0739688858000257, `1041` = -0.0423164099136734,
`2001` = 0.157510857198006, `2002` = 0.07637420233757, `2003` = 0.155257829487471,
`2004` = 0.0572168560821465, `2006` = -0.208582520792082,
`2007` = -0.0177446511035586, `2010` = -0.124058959053879,
`2011` = 0.0418804695467237, `2012` = 0.099707284045702,
`2013` = 0.0359360286074771, `2014` = 0.0443211605035362,
`2016` = -0.13800300853893, `2018` = -0.0498636395982077,
`3001` = 0.0792281291001036, `3002` = 0.0802210096384973,
`3004` = 0.121633097720208, `3005` = 0.0565084671537361,
`3006` = -0.0579659743139385, `4001` = 0.0156785346591978,
`4006` = 0.0993721443703216, `4007` = -0.183195864294347,
`5001` = -0.0122235224182978, `5003` = -0.183684629956499,
`5012` = -0.16755820464269, `5014` = 0.143741030413913, `5018` = -0.141967798211089,
`5021` = 0.0306606708795011, `5027` = 0.179594762371493,
`5028` = 0.120975533264822, `5029` = -0.129648450802276,
`5031` = 0.0509203479149378, `6001` = 0.247698053357657,
`6003` = -0.0067528112519499, `6005` = -0.123955921792593,
`6008` = -0.0504640448301967, `6011` = 0.142375204051896,
`6012` = 0.136298047052455, `6015` = 0.141367201888835, `6017` = -0.0560805093001182,
`6018` = -0.146050167802696, `6020` = -0.229903190067814,
`6022` = 0.147507242415981, `6025` = -0.0335256628186052,
`6026` = -0.0806432468946914, `7008` = 0.056379262737679,
`7010` = 0.0253746420351423, `8003` = 0.0693474302565438,
`8006` = -0.0536899517817604, `9001` = 0.0475058919529396,
`9004` = 0.0543135016213903, `9005` = 0.0213019529348318,
`9011` = -0.126252808227583, `9013` = -0.0278767686185694
), .Dim = 73L, .Dimnames = list(c("1002", "1007", "1008",
"1011", "1012", "1013", "1014", "1015", "1018", "1019", "1021",
"1023", "1025", "1031", "1033", "1034", "1035", "1036", "1037",
"1041", "2001", "2002", "2003", "2004", "2006", "2007", "2010",
"2011", "2012", "2013", "2014", "2016", "2018", "3001", "3002",
"3004", "3005", "3006", "4001", "4006", "4007", "5001", "5003",
"5012", "5014", "5018", "5021", "5027", "5028", "5029", "5031",
"6001", "6003", "6005", "6008", "6011", "6012", "6015", "6017",
"6018", "6020", "6022", "6025", "6026", "7008", "7010", "8003",
"8006", "9001", "9004", "9005", "9011", "9013"))), coefficients = structure(c(-0.00397153563921837,
0.341578798274534, -0.139319915370845, 0.000896071157731704,
1.09794729645418, 0.015147052709038, 0.092737093652735, 0.145373948252719,
0.000447094773201659, 0.963032924564176, -0.262198575228343,
3.68330281681693, -0.958355448451128, 2.00420852902152, 1.14009320808119,
0.793961023095372, 0.000457621377693468, 0.341278649254439,
0.0490327040240946, 0.25824730720917), .Dim = 5:4, .Dimnames = list(
c("(Intercept)", "b", "c", "d", "b:c"), c("Estimate",
"Std. Error", "t value", "Pr(>|t|)"))), aliased = c(`(Intercept)` = FALSE,
b = FALSE, c = FALSE, d = FALSE, `b:c` = FALSE), sigma = 0.12089767430346,
df = c(5L, 68L, 5L), r.squared = 0.230894512279432, adj.r.squared = 0.185653013001751,
fstatistic = c(value = 5.10359992409318, numdf = 4, dendf = 68
), cov.unscaled = structure(c(0.0156971345336508, 0.00228620672711246,
-0.0177546074340744, -0.000138674927777783, 0.00210552637725186,
0.00228620672711246, 0.58839783944054, -0.00305591977935581,
-5.18791289583915e-05, -0.586754865532256, -0.0177546074340744,
-0.00305591977935581, 1.44589673996892, 0.00034158306687199,
-8.28386215614676, -0.000138674927777783, -5.18791289583915e-05,
0.00034158306687199, 1.36761322730522e-05, -0.000381233934849903,
0.00210552637725186, -0.586754865532256, -8.28386215614676,
-0.000381233934849903, 63.4521551547907), .Dim = c(5L, 5L
), .Dimnames = list(c("(Intercept)", "b", "c", "d", "b:c"
), c("(Intercept)", "b", "c", "d", "b:c")))), class = "summary.lm"),
`10005` = structure(list(call = lm(formula = a ~ b * c +
d), terms = a ~ b * c + d, residuals = structure(c(`1002` = 0.0187421617609578,
`1007` = -0.188448583704918, `1008` = 0.153893088127834,
`1011` = -0.0457630912535438, `1012` = 0.0404843307903381,
`1013` = 0.130151895395599, `1014` = 0.114287043586286, `1015` = 0.113657175701791,
`1018` = -0.0688198422536628, `1019` = -0.300487949059847,
`1021` = 0.427885578924476, `1023` = 0.114428274713097, `1025` = -0.278157663045114,
`1031` = 0.213947755501182, `1033` = -0.0504476261677984,
`1034` = 0.0699549135624495, `1035` = -0.0849815620672379,
`1036` = -0.137089410376454, `1037` = 0.450333414230324,
`1041` = -0.163440857416823, `2001` = -0.00124584145399394,
`2002` = 0.391009123413889, `2003` = -0.306817622497255,
`2004` = -0.0509569271622155, `2006` = -0.356474495908554,
`2007` = -0.0872195400782593, `2010` = 0.159666418255182,
`2011` = -0.015748357988549, `2012` = -0.419884099549585,
`2013` = 0.0182516508985773, `2014` = -0.382996383712274,
`2016` = -0.172159567245906, `2018` = 0.0445901611973861,
`3001` = 0.128229814452709, `3002` = 0.255302353394398, `3004` = -0.241955175096404,
`3005` = -0.575641150113639, `3006` = -0.0910796612018084,
`4001` = -0.0620198072407937, `4006` = 0.143912701831265,
`4007` = 0.0808817918985259, `5001` = -0.187192834816796,
`5003` = 0.568476264401867, `5012` = 0.300702938570628, `5014` = -0.4560350408085,
`5018` = -0.00848653960194647, `5021` = 0.00957704877249509,
`5027` = -0.24110496425874, `5028` = -0.345492109461339,
`5029` = 0.249405987254773, `5031` = 0.0320913446994224,
`6001` = -0.0351426453859569, `6003` = 0.335221232977152,
`6005` = -0.0825636357399562, `6008` = 0.0244166685815588,
`6011` = -0.228748294884997, `6012` = -0.0953103316656939,
`6015` = 0.179929598504696, `6017` = 0.240092861975075, `6018` = 0.236650982207435,
`6020` = 0.34790759483171, `6022` = 0.180188611758191, `6025` = 0.199766542093749,
`6026` = 0.189429952399034, `7008` = -0.0556828704790727,
`7010` = -0.0208925809968781, `8003` = -0.172382119699295,
`8006` = -0.0616931970974188, `9001` = 0.0628803668679693,
`9004` = -0.241658058098228, `9005` = -0.0935380282058113,
`9011` = 0.348007141737809, `9013` = -0.166596319474566), .Dim = 73L, .Dimnames = list(
c("1002", "1007", "1008", "1011", "1012", "1013", "1014",
"1015", "1018", "1019", "1021", "1023", "1025", "1031",
"1033", "1034", "1035", "1036", "1037", "1041", "2001",
"2002", "2003", "2004", "2006", "2007", "2010", "2011",
"2012", "2013", "2014", "2016", "2018", "3001", "3002",
"3004", "3005", "3006", "4001", "4006", "4007", "5001",
"5003", "5012", "5014", "5018", "5021", "5027", "5028",
"5029", "5031", "6001", "6003", "6005", "6008", "6011",
"6012", "6015", "6017", "6018", "6020", "6022", "6025",
"6026", "7008", "7010", "8003", "8006", "9001", "9004",
"9005", "9011", "9013"))), coefficients = structure(c(-0.0215512238688343,
0.882664068869997, 0.112689386248365, 3.18086709307044e-05,
0.323131323928193, 0.0331228590383073, 0.0930593548886002,
0.0622430065698782, 0.000869130701922098, 0.204375349678183,
-0.650645037733908, 9.48495795964435, 1.81047466146823, 0.036598259456672,
1.58106799296983, 0.517468135410013, 4.52946909054405e-14,
0.0746401933888581, 0.970912559215124, 0.118503083862495), .Dim = 5:4, .Dimnames = list(
c("(Intercept)", "b", "c", "d", "b:c"), c("Estimate",
"Std. Error", "t value", "Pr(>|t|)"))), aliased = c(`(Intercept)` = FALSE,
b = FALSE, c = FALSE, d = FALSE, `b:c` = FALSE), sigma = 0.235512709292802,
df = c(5L, 68L, 5L), r.squared = 0.658368394847432, adj.r.squared = 0.638272418073752,
fstatistic = c(value = 32.7612040092369, numdf = 4, dendf = 68
), cov.unscaled = structure(c(0.019780029532837, -0.00791428965948656,
-0.0170945331340147, -0.000152915994786481, 0.0135606288377018,
-0.00791428965948656, 0.156131804131347, 0.0104146731175132,
-2.91371264679668e-06, -0.14991987347644, -0.0170945331340147,
0.0104146731175132, 0.0698477511652982, 0.000105323084392133,
0.0396223016159103, -0.000152915994786481, -2.91371264679668e-06,
0.000105323084392133, 1.36188829142178e-05, -2.24663759602837e-05,
0.0135606288377018, -0.14991987347644, 0.0396223016159103,
-2.24663759602837e-05, 0.753057831011354), .Dim = c(5L,
5L), .Dimnames = list(c("(Intercept)", "b", "c", "d",
"b:c"), c("(Intercept)", "b", "c", "d", "b:c")))), class = "summary.lm"))
将您的函数更改为 -
func_coef_intercept <- function(x) x[["coefficients"]][1,1]
然后您就可以使用您的代码了。 -
coef_intercept <- lapply(list, func_coef_intercept)
我有一个列表,其中包含不同线性回归的摘要。我想提取线性回归所有元素的系数和 p 值,并将它们放入一个数据帧中(每个 lm 元素一个数据帧)。
例如,要提取截距的系数,我使用以下方法:
func_coef_intercept <- function(x) list[[x]][["coefficients"]][1,1]
coef_intercept <- lapply(list, func_coef_intercept)
要提取 p 值:
func_coef_intercept <- function(x) list[[x]][["coefficients"]][1,4]
pvalue_intercept <- lapply(list, func_coef_intercept)
但是我遇到了这个错误
Error in list[[x]] : invalid subscript type 'list'
如何提取线性回归的所有元素的系数?
这是列表前两个摘要的示例:
list(`1` = structure(list(call = lm(formula = a ~ b * c + d),
terms = a ~ b * c + d, residuals = structure(c(`1002` = 0.0118681256952383,
`1007` = 0.0419256575878256, `1008` = -0.0963910775101885,
`1011` = 0.22304807403332, `1012` = -0.167863579846024, `1013` = -0.0613889863570078,
`1014` = 0.0357270798843175, `1015` = -0.172203913653826,
`1018` = -0.156689137194, `1019` = -0.129444770996805, `1021` = -0.175019531525519,
`1023` = 0.146466383569852, `1025` = 0.110813999746634, `1031` = -0.0920873415380558,
`1033` = 0.000742586091925349, `1034` = 0.181961137021771,
`1035` = 0.146326238597575, `1036` = -0.118020186381677,
`1037` = -0.0739688858000257, `1041` = -0.0423164099136734,
`2001` = 0.157510857198006, `2002` = 0.07637420233757, `2003` = 0.155257829487471,
`2004` = 0.0572168560821465, `2006` = -0.208582520792082,
`2007` = -0.0177446511035586, `2010` = -0.124058959053879,
`2011` = 0.0418804695467237, `2012` = 0.099707284045702,
`2013` = 0.0359360286074771, `2014` = 0.0443211605035362,
`2016` = -0.13800300853893, `2018` = -0.0498636395982077,
`3001` = 0.0792281291001036, `3002` = 0.0802210096384973,
`3004` = 0.121633097720208, `3005` = 0.0565084671537361,
`3006` = -0.0579659743139385, `4001` = 0.0156785346591978,
`4006` = 0.0993721443703216, `4007` = -0.183195864294347,
`5001` = -0.0122235224182978, `5003` = -0.183684629956499,
`5012` = -0.16755820464269, `5014` = 0.143741030413913, `5018` = -0.141967798211089,
`5021` = 0.0306606708795011, `5027` = 0.179594762371493,
`5028` = 0.120975533264822, `5029` = -0.129648450802276,
`5031` = 0.0509203479149378, `6001` = 0.247698053357657,
`6003` = -0.0067528112519499, `6005` = -0.123955921792593,
`6008` = -0.0504640448301967, `6011` = 0.142375204051896,
`6012` = 0.136298047052455, `6015` = 0.141367201888835, `6017` = -0.0560805093001182,
`6018` = -0.146050167802696, `6020` = -0.229903190067814,
`6022` = 0.147507242415981, `6025` = -0.0335256628186052,
`6026` = -0.0806432468946914, `7008` = 0.056379262737679,
`7010` = 0.0253746420351423, `8003` = 0.0693474302565438,
`8006` = -0.0536899517817604, `9001` = 0.0475058919529396,
`9004` = 0.0543135016213903, `9005` = 0.0213019529348318,
`9011` = -0.126252808227583, `9013` = -0.0278767686185694
), .Dim = 73L, .Dimnames = list(c("1002", "1007", "1008",
"1011", "1012", "1013", "1014", "1015", "1018", "1019", "1021",
"1023", "1025", "1031", "1033", "1034", "1035", "1036", "1037",
"1041", "2001", "2002", "2003", "2004", "2006", "2007", "2010",
"2011", "2012", "2013", "2014", "2016", "2018", "3001", "3002",
"3004", "3005", "3006", "4001", "4006", "4007", "5001", "5003",
"5012", "5014", "5018", "5021", "5027", "5028", "5029", "5031",
"6001", "6003", "6005", "6008", "6011", "6012", "6015", "6017",
"6018", "6020", "6022", "6025", "6026", "7008", "7010", "8003",
"8006", "9001", "9004", "9005", "9011", "9013"))), coefficients = structure(c(-0.00397153563921837,
0.341578798274534, -0.139319915370845, 0.000896071157731704,
1.09794729645418, 0.015147052709038, 0.092737093652735, 0.145373948252719,
0.000447094773201659, 0.963032924564176, -0.262198575228343,
3.68330281681693, -0.958355448451128, 2.00420852902152, 1.14009320808119,
0.793961023095372, 0.000457621377693468, 0.341278649254439,
0.0490327040240946, 0.25824730720917), .Dim = 5:4, .Dimnames = list(
c("(Intercept)", "b", "c", "d", "b:c"), c("Estimate",
"Std. Error", "t value", "Pr(>|t|)"))), aliased = c(`(Intercept)` = FALSE,
b = FALSE, c = FALSE, d = FALSE, `b:c` = FALSE), sigma = 0.12089767430346,
df = c(5L, 68L, 5L), r.squared = 0.230894512279432, adj.r.squared = 0.185653013001751,
fstatistic = c(value = 5.10359992409318, numdf = 4, dendf = 68
), cov.unscaled = structure(c(0.0156971345336508, 0.00228620672711246,
-0.0177546074340744, -0.000138674927777783, 0.00210552637725186,
0.00228620672711246, 0.58839783944054, -0.00305591977935581,
-5.18791289583915e-05, -0.586754865532256, -0.0177546074340744,
-0.00305591977935581, 1.44589673996892, 0.00034158306687199,
-8.28386215614676, -0.000138674927777783, -5.18791289583915e-05,
0.00034158306687199, 1.36761322730522e-05, -0.000381233934849903,
0.00210552637725186, -0.586754865532256, -8.28386215614676,
-0.000381233934849903, 63.4521551547907), .Dim = c(5L, 5L
), .Dimnames = list(c("(Intercept)", "b", "c", "d", "b:c"
), c("(Intercept)", "b", "c", "d", "b:c")))), class = "summary.lm"),
`10005` = structure(list(call = lm(formula = a ~ b * c +
d), terms = a ~ b * c + d, residuals = structure(c(`1002` = 0.0187421617609578,
`1007` = -0.188448583704918, `1008` = 0.153893088127834,
`1011` = -0.0457630912535438, `1012` = 0.0404843307903381,
`1013` = 0.130151895395599, `1014` = 0.114287043586286, `1015` = 0.113657175701791,
`1018` = -0.0688198422536628, `1019` = -0.300487949059847,
`1021` = 0.427885578924476, `1023` = 0.114428274713097, `1025` = -0.278157663045114,
`1031` = 0.213947755501182, `1033` = -0.0504476261677984,
`1034` = 0.0699549135624495, `1035` = -0.0849815620672379,
`1036` = -0.137089410376454, `1037` = 0.450333414230324,
`1041` = -0.163440857416823, `2001` = -0.00124584145399394,
`2002` = 0.391009123413889, `2003` = -0.306817622497255,
`2004` = -0.0509569271622155, `2006` = -0.356474495908554,
`2007` = -0.0872195400782593, `2010` = 0.159666418255182,
`2011` = -0.015748357988549, `2012` = -0.419884099549585,
`2013` = 0.0182516508985773, `2014` = -0.382996383712274,
`2016` = -0.172159567245906, `2018` = 0.0445901611973861,
`3001` = 0.128229814452709, `3002` = 0.255302353394398, `3004` = -0.241955175096404,
`3005` = -0.575641150113639, `3006` = -0.0910796612018084,
`4001` = -0.0620198072407937, `4006` = 0.143912701831265,
`4007` = 0.0808817918985259, `5001` = -0.187192834816796,
`5003` = 0.568476264401867, `5012` = 0.300702938570628, `5014` = -0.4560350408085,
`5018` = -0.00848653960194647, `5021` = 0.00957704877249509,
`5027` = -0.24110496425874, `5028` = -0.345492109461339,
`5029` = 0.249405987254773, `5031` = 0.0320913446994224,
`6001` = -0.0351426453859569, `6003` = 0.335221232977152,
`6005` = -0.0825636357399562, `6008` = 0.0244166685815588,
`6011` = -0.228748294884997, `6012` = -0.0953103316656939,
`6015` = 0.179929598504696, `6017` = 0.240092861975075, `6018` = 0.236650982207435,
`6020` = 0.34790759483171, `6022` = 0.180188611758191, `6025` = 0.199766542093749,
`6026` = 0.189429952399034, `7008` = -0.0556828704790727,
`7010` = -0.0208925809968781, `8003` = -0.172382119699295,
`8006` = -0.0616931970974188, `9001` = 0.0628803668679693,
`9004` = -0.241658058098228, `9005` = -0.0935380282058113,
`9011` = 0.348007141737809, `9013` = -0.166596319474566), .Dim = 73L, .Dimnames = list(
c("1002", "1007", "1008", "1011", "1012", "1013", "1014",
"1015", "1018", "1019", "1021", "1023", "1025", "1031",
"1033", "1034", "1035", "1036", "1037", "1041", "2001",
"2002", "2003", "2004", "2006", "2007", "2010", "2011",
"2012", "2013", "2014", "2016", "2018", "3001", "3002",
"3004", "3005", "3006", "4001", "4006", "4007", "5001",
"5003", "5012", "5014", "5018", "5021", "5027", "5028",
"5029", "5031", "6001", "6003", "6005", "6008", "6011",
"6012", "6015", "6017", "6018", "6020", "6022", "6025",
"6026", "7008", "7010", "8003", "8006", "9001", "9004",
"9005", "9011", "9013"))), coefficients = structure(c(-0.0215512238688343,
0.882664068869997, 0.112689386248365, 3.18086709307044e-05,
0.323131323928193, 0.0331228590383073, 0.0930593548886002,
0.0622430065698782, 0.000869130701922098, 0.204375349678183,
-0.650645037733908, 9.48495795964435, 1.81047466146823, 0.036598259456672,
1.58106799296983, 0.517468135410013, 4.52946909054405e-14,
0.0746401933888581, 0.970912559215124, 0.118503083862495), .Dim = 5:4, .Dimnames = list(
c("(Intercept)", "b", "c", "d", "b:c"), c("Estimate",
"Std. Error", "t value", "Pr(>|t|)"))), aliased = c(`(Intercept)` = FALSE,
b = FALSE, c = FALSE, d = FALSE, `b:c` = FALSE), sigma = 0.235512709292802,
df = c(5L, 68L, 5L), r.squared = 0.658368394847432, adj.r.squared = 0.638272418073752,
fstatistic = c(value = 32.7612040092369, numdf = 4, dendf = 68
), cov.unscaled = structure(c(0.019780029532837, -0.00791428965948656,
-0.0170945331340147, -0.000152915994786481, 0.0135606288377018,
-0.00791428965948656, 0.156131804131347, 0.0104146731175132,
-2.91371264679668e-06, -0.14991987347644, -0.0170945331340147,
0.0104146731175132, 0.0698477511652982, 0.000105323084392133,
0.0396223016159103, -0.000152915994786481, -2.91371264679668e-06,
0.000105323084392133, 1.36188829142178e-05, -2.24663759602837e-05,
0.0135606288377018, -0.14991987347644, 0.0396223016159103,
-2.24663759602837e-05, 0.753057831011354), .Dim = c(5L,
5L), .Dimnames = list(c("(Intercept)", "b", "c", "d",
"b:c"), c("(Intercept)", "b", "c", "d", "b:c")))), class = "summary.lm"))
将您的函数更改为 -
func_coef_intercept <- function(x) x[["coefficients"]][1,1]
然后您就可以使用您的代码了。 -
coef_intercept <- lapply(list, func_coef_intercept)