How to store the results of a function in a data.frame?

Asked

Viewed 68 times

0

Good afternoon

I am using the R to make a temporal analysis of the Infant Mortality Coefficients (IMR) of each Administrative District (n = 93) of São Paulo between 2013 and 2019

The base table I am using has the Years in the rows and each district (93) is a column.

I am running a time analysis algorithm, "Prais_winsten", and it returns a list to me as a result. From this list, three values are very important and need to use them in a formula in Excel to calculate equations of interest.

In this case, I am running for 93 lines (administrative districts) and keeping the lists that result in a large list I called pw_results

Then, I check them one by one, taking the values I need and reproducing them in a formula in Excel. I’m starting in the R but has helped me a lot in the routines of working with data. I am an intern at the Health Department of the Municipality of São Paulo.

Well, let me show you my simple script.

library(readxl)
library(prais)

CMI <- read_excel("cmi_da.xlsx", sheet = "CMI_DA")
CMI_lg <- read_excel("cmi_da.xlsx", sheet = "CMI_DA")

dataset_length <- length(names(CMI_lg))



pw_resultados <- c()
pw_resultados <- as.list(pw_resultados)


 for (i in 2:dataset_length) {
  CMI_lg[,i] <- log10(CMI_lg[,i])
}


 for (a in 2:dataset_length) {
    pw_resultados[[a]] <- summary(prais_winsten(as.vector(CMI_lg[,a]) ~ Ano, data = CMI_lg))
     }

Well, the code runs smoothly and the only annoying thing is that the manual work of checking the results one by one is quite time consuming and it takes part of my time that could use analyzing the results.

pw_resultados[1]
pw_resultados[2]
pw_resultados[3]

#[...]

pw_resultados[93]

In each of these commands R returns me to the following list:

pw_resultados[2]
#[[1]]
#
3Call:
#prais_winsten(formula = as.vector(CMI_lg[, a]) ~ Ano, data = CMI_lg)
#
#
#AR(1) coefficient rho after 5 Iterations: -0.6745
#
#Coefficients:
#             Estimate Std. Error t value Pr(>|t|)  
#(Intercept) 119.00927   35.23810   3.377   0.0197 *
#Ano          -0.05864    0.01748  -3.355   0.0202 *
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#Residual standard error: 0.1286 on 5 degrees of freedom
#Multiple R-squared:  0.8741,   Adjusted R-squared:  0.8489 
#F-statistic:  34.7 on 1 and 5 DF,  p-value: 0.002004
#
#Durbin-Watson statistic (original):  3.23 
#Durbin-Watson statistic (transformed): 1.945

Of all this, what interests me are the values of the second line of the Coefficients

#Coefficients:
#             Estimate Std. Error t value Pr(>|t|)  
#(Intercept) 119.00927   35.23810   3.377   0.0197 *
#Ano          -0.05864   *0.01748  -3.355   *0.0202* *
#---

I needed to generate a data.frame with the same indexing of the data table that I used to run prais_winsten that kept these values that I imagine to be pw_results[a]$coefficients[2,] Where each district ( or column ) received the respective values of interest of the prais_winsten function

Could someone help me?

I tried to look at the code of the prais_winsten function to see if I could change something but without success.

Whoever wants, just type in the R:

getAnywhere(prais_winsten())

To reproduce the data and run the code!

dput(head(CMI_lg, 20))

structure(list(Ano = c(2013, 2014, 2015, 2016, 2017, 2018, 2019
), `Água Rasa` = c(0.889410289700751, 1.08051959681989, 0.74072475244302, 
0.913244817015834, 0.471083299722345, 0.793623937039851, 0.703334809738469
), `Alto de Pinheiros` = c(0.755722879198157, 0.977434172101259, 
0.518557371497695, 1.14721513131945, 0.838631997765025, 0.8843894883257, 
0.508638306165727), Anhanguera = c(1.09652242308326, 1.06472532963666, 
1.18009065048461, 1.02561392876918, 1.12726117252733, 0.739531088433941, 
0.87168152274032), Aricanduva = c(1.00363428465509, 1.00290499064341, 
0.807571944668793, 0.925183559354825, 1.02495133618423, 0.927750102386485, 
0.862092294568453), `Artur Alvim` = c(0.943412731479803, 1.03842072181893, 
1.03747282515619, 1.25701316667961, 1.32858714927555, 0.940677598585143, 
0.615467384505751), `Bela Vista` = c(1.05109823902979, 0.85959671983337, 
0.748118545447472, 0.707743928643524, 1.09258863922541, 0.362510270487489, 
1.15146878414735), Belém = c(0.475820321699244, 0.756135510565923, 
1.1204126517769, 1.10068468702332, 1.10110612760166, 0.947776467190093, 
0.800427645094796), `Bom Retiro` = c(0.968995718636463, 1.45017962676063, 
1.19044028536473, 0.552841968657781, 1.0276428555323, 1.1525815921406, 
0.761953896871205), Brás = c(1.2068307015529, 1.32483291033661, 
1.16510266065851, 0.892790030352132, 0.742321425130815, 0.939302159646388, 
1.28981751839029), Brasilândia = c(1.07666961476272, 1.12318754680854, 
1.18985152544628, 1.1808949674585, 1.07429679632844, 1.12575380312574, 
1.27670073184943), Cachoeirinha = c(1.10415513068501, 1.06314081917895, 
1.114450324994, 1.0122699104749, 1.13893405763383, 1.03794651487619, 
1.08182499483207), Cambuci = c(0.756961951313706, 0.872895201635192, 
1.0215151421378, 0.772113295386326, 0.999132278468773, 1.05469557834846, 
0.76870035458495), `Campo Belo` = c(0.944184972276588, 0.564899198187247, 
0.916190349770304, 0.951336277956313, 0.896196279044043, 0.997294106624075, 
0.862224038686528), `Campo Grande` = c(1.02064676997764, 0.983288003284806, 
0.694341600153948, 0.610125441609014, 0.98751428845194, 1.03803159082406, 
1.15308101609412), `Campo Limpo` = c(1.05263801687069, 1.07630863129053, 
1.03072049380803, 1.09236881130041, 1.06734178236535, 0.944942426999081, 
1.01968946772596), Cangaíba = c(0.97255331063729, 1.16880798731659, 
1.009328224934, 1.10769922855089, 1.04383156952464, 1.00656376950239, 
1.11476446206513), `Capão Redondo` = c(1.10658621149447, 1.02218310838668, 
1.06634402134245, 1.06923352826959, 1.02647911969302, 1.15885911248316, 
1.02451475630372), Carrão = c(1.17870487465837, 0.790932020475639, 
0.996108833763089, 0.831698985370571, 0.835647144215563, 0.903089986991944, 
1.04042865705561), `Casa Verde` = c(1.17050703331632, 0.98410704074001, 
0.797044866898648, 1.16869024447138, 1.11699410381146, 0.940562812148132, 
1.22184874961636), `Cidade Ademar` = c(1.0386254416402, 0.938830356795095, 
1.01161184215075, 1.01031808409215, 0.912103522054163, 1.05733403992828, 
1.16494389827988), `Cidade Dutra` = c(1.15078539379091, 0.830717528496593, 
1.12104757037139, 1.10852449147267, 1.10039226038454, 0.919091310235361, 
1.07789428084845), `Cidade Líder` = c(1.06902883297366, 1.01051149067825, 
0.823082300196537, 1.05082220664145, 1.01978996912998, 1.04849354432287, 
1.10612732120492), `Cidade Tiradentes` = c(1.17540607172149, 
1.1661717106378, 1.12968281486541, 1.11952940719622, 1.17881411739115, 
1.19263115453209, 1.06779207319325), Consolação = c(0.382999658879101, 
1.2518119729938, 0.981923936354205, 0.700057099977233, 1.0268721464003, 
0.404503778174426, 0.911273436046145), Cursino = c(1.11132138867934, 
1.14013516705334, 0.90626003378145, 0.837966921858781, 0.818156412055227, 
1.00639422256215, 1.04438096173187), `Ermelino Matarazzo` = c(1.19288505368104, 
0.96301643374683, 1.06184431045413, 0.965631098368738, 1.25326852376738, 
0.961578554357541, 1.24544079053786), `Freguesia do Ó` = c(1.03486552201917, 
1.02561392876918, 0.945855279325166, 1.21905584943219, 1.12355121312166, 
1.05340739099414, 1.02389287727924), Grajaú = c(1.07101219697208, 
1.08463120688645, 1.07870228875408, 1.11528735093212, 1.13838933469586, 
1.07457330380727, 1.10196588479794), Guaianases = c(0.921031695600134, 
1.13688966470977, 1.16567014340214, 0.997720233705101, 1.13033376849501, 
1.07314329105031, 1.21372192338566), Iguatemi = c(1.20466891192459, 
1.17016039183647, 1.04558518566577, 1.18082178614871, 1.09187257231644, 
1.1128048229587, 1.10827674789384), Ipiranga = c(0.870837626637017, 
0.711080394338273, 1.01378828448563, 1.0248555183169, 0.809668301829709, 
0.958911123515867, 0.904672118001818), `Itaim Bibi` = c(0.639406586434751, 
-0.0927206446840991, 0.757671582977971, 0.626720106722504, 0.664542099310616, 
0.194159451185327, 0.592779107072604), `Itaim Paulista` = c(1.09997443210192, 
1.15192090061068, 1.16626031504122, 1.09350099835296, 1.24955094715625, 
1.16066701563953, 1.08053921309315), Itaquera = c(1.13210416099597, 
1.17089323079309, 0.996691558412763, 0.99891561870778, 1.12011765677363, 
1.02214195902461, 1.07210273193442), Jabaquara = c(0.927573081101806, 
1.03165858460094, 0.970906235335186, 1.06859863760356, 0.848148791518876, 
0.838631997765025, 0.905147883412083), Jaçanã = c(1.0413258757616, 
1.22774816609227, 1.15417266597406, 1.00473052709303, 1.09799710864927, 
1.16115090926274, 1.14125055457313), Jaguara = c(0.505845405981557, 
1.1322379753498, 0.530177984021837, 0.54515513999149, 0.473660722610156, 
1.20901152491118, 0.846185135655471), Jaguaré = c(0.994604968113294, 
0.967582721167231, 0.848221690299056, 0.872895201635192, 1.05305672930217, 
0.968086835538289, 0.936613021135607), Jaraguá = c(1.11796361625377, 
1.14508697769214, 1.10933207898702, 1.15027355580367, 0.950648286553973, 
1.09137390771361, 1.07816907266677), `Jardim Ângela` = c(1.08373144931069, 
0.966648058400259, 1.11508280728043, 1.0730282410743, 0.947439234121226, 
0.976087842594589, 1.12306475535369), `Jardim Helena` = c(1.09860979788595, 
1.08701447989467, 1.04442835568422, 1.13041273830737, 1.10790539730952, 
1.15014203776444, 1.18597449298778), `Jardim Paulista` = c(0.359518563029578, 
0.83813309537598, 0.844663962534938, -3, 0.657577319177794, 0.786747947803603, 
0.106793246940152), `Jardim São Luís` = c(1.04187344718329, 1.03470820780651, 
1.0159621429031, 1.0332185011518, 1.13503906351263, 1.03684204148407, 
1.036651519664), `José Bonifácio` = c(1.08392222685853, 0.986419332348252, 
1.18782966232693, 1.1464538787461, 1.05525765999043, 1.2767496196169, 
1), Lajeado = c(1.13407310120698, 1.12301281557226, 1.16610489243066, 
1.22450157739747, 1.13667713987954, 1.17062474424858, 1.00300551013973
), Lapa = c(0.806875401645538, 0.554914977280646, 1.13130213956416, 
0.596879478824182, 0.791021482723747, 0.593459819566045, 0.702786804010359
), Liberdade = c(0.964856264118776, 0.399571674267869, 0.895626712428763, 
0.97061622231479, 0.931814138253838, 0.964856264118776, 0.912455190467573
), Limão = c(0.75913887056798, 1.23284413391782, 1.06994287507916, 
1.13386818266569, 0.755546157127803, 1.04836363757839, 1.04495398527707
), Mandaqui = c(0.981923936354205, 0.98471011740598, 0.900664722314042, 
1.02333264447843, 0.889410289700751, 0.95272513261582, 0.661942124580244
), Moema = c(0.0390538042661685, 0.670602120638957, 0.902098446648216, 
0.580044251510242, 0.66354026615147, 0.546172223552139, 0.396855627379818
), Mooca = c(0.786039762596694, 0.722620025332745, 0.837350262013517, 
0.653647025549361, 1.0301183562535, 0.882728704344236, 0.981923936354205
), Morumbi = c(0.70060166993185, 0.599749908849888, 0.603800652904264, 
0.761953896871205, 0.890759031411797, 0.903785414653595, 1.22112552799726
), Parelheiros = c(1.2528472404254, 1.07692876612709, 1.00485250279441, 
1.10154076727485, 1.00530585556893, 1.05322033619125, 1.11517132998237
), Pari = c(0.988570538219218, 1.07701518429112, 1.37380632895532, 
1.14569395819892, 1.10237290870956, 1.18842499412941, 1.15490195998574
), `Parque do Carmo` = c(1.10964885401428, 1.19893947015214, 
1.28330122870355, 1.12675207737177, 1.09799710864927, 1.29132051427747, 
1.0173176978508), Pedreira = c(1.07589308722155, 0.968328140724491, 
0.998194199136738, 0.954490748646943, 1.0924403905529, 1.14014534849493, 
1.0434474080826), Penha = c(1.12731409976911, 1.1518108830086, 
1.10605339244793, 0.995409962749524, 0.913131925286084, 0.859334860023264, 
1.05771629194977), Perus = c(1.17333632333518, 1.28515016164333, 
0.941048654554661, 1.12365384075418, 1.27748359728341, 1.03151705144606, 
1.03352303414366), Pinheiros = c(0.18309616062434, 0.484126156288321, 
0.202040356262804, 0.493494967595128, 0.645891560852599, 0.484788695672198, 
0.685430605699544), Pirituba = c(1.16476252659969, 0.988923193116309, 
1.05826777708026, 0.972941767265964, 1.09052321434666, 1.0374453807199, 
1.12951248812106), `Ponte Rasa` = c(1.00504022463952, 1.10031626169997, 
1.12732966783256, 0.986779630601337, 1.20605448243312, 1.1644959410644, 
1.04215336629185), `Raposo Tavares` = c(0.731422028117157, 1.10100072911021, 
1.10597304937455, 1.0232398881333, 0.931566747485597, 1.01572278426283, 
0.832951328719658), República = c(1.34678748622466, 1.31811297145426, 
1.20481541031758, 1.19156394571189, 1.01999662841625, 1.38457604711406, 
0.807571944668793), `Rio Pequeno` = c(1.01062435097526, 0.901933409792001, 
1.04195401146535, 0.875944321217602, 0.847915597617359, 1.11373494097024, 
1.00534074846911), Sacomã = c(1.01606525665113, 0.962105007427209, 
1.03197288405879, 1.03488276037359, 0.879060654820633, 1.03151705144606, 
0.984273596631564), `Santa Cecília` = c(1.14417809459397, 0.825358807339552, 
0.974182296747991, 1.12090412049993, 0.937686679239671, 0.67726399553005, 
0.983488963207833), Santana = c(0.997667764726292, 0.75448733218585, 
1.04985991634324, 0.967307996129981, 1.04427525539524, 0.92959267825988, 
0.991399828238082), `Santo Amaro` = c(0.340559218129682, 0.991873173769304, 
0.964570261815452, 0.918241331064565, 0.894733185638134, 0.67520328237827, 
1.06651271215129), `São Domingos` = c(0.777655197237876, 1.02368529937552, 
0.991045157347074, 1.20180990017785, 1.15645578805436, 0.739531088433941, 
0.923642787473374), `São Lucas` = c(0.982230360188199, 1.21872345062415, 
1.15801519540989, 0.914913387715637, 1.03976712687149, 1.10106597277921, 
0.917767167266863), `São Mateus` = c(1.2277140803878, 1.09972099034188, 
0.997114311762512, 1.11826570675555, 1.07643442729826, 1.1178979722019, 
0.964665574990838), `São Miguel` = c(0.981923936354205, 1.07340034609297, 
0.957293260943944, 0.711080394338273, 1.07409108920461, 1.03899790691338, 
1.30697693207631), `São Rafael` = c(1.12737386408578, 1.09151498112135, 
1.05971344244344, 1.05990514162197, 1.13096395248765, 1.07706320181363, 
1.15277448331939), Sapopemba = c(1.0546299055097, 1.01183756858322, 
1.00232243591903, 1.06267020925546, 1.05520898954657, 1.12048216546383, 
1.08652433931299), Saúde = c(0.204467557289846, 0.926781700926052, 
0.599634726650061, 0.821023052706831, 0.741636417919552, 0.817799408761879, 
0.818871300252705), Socorro = c(0.704432900037521, 1.08407278830288, 
1.0054628957015, 1.20065945054642, 0.405607449624573, 1.26875668980846, 
1.28122160231043), Tatuapé = c(0.945004138470859, 0.874751163784164, 
0.931350396104165, 1.03715731879876, 1.03668448861389, 1.03621217265444, 
0.727230413448241), Tremembé = c(1.0333369052753, 1.1200762254967, 
1.05933029774995, 1.18045606445813, 1.13487026572887, 0.981143532762533, 
1.21225890010466), Tucuruvi = c(0.897480541631663, 0.928527125537984, 
1.00796892967128, 0.91721462968355, 0.980883709552927, 0.698102282804792, 
1.0204516252959), `Vila Andrade` = c(0.934295827760483, 0.894319537054191, 
0.922199659668761, 0.789770685161635, 0.953611267348683, 0.928955267836381, 
0.649399612898512), `Vila Curuçá` = c(1.10060316509556, 1.12022663452049, 
1.01239993345536, 1.09999721237321, 1.2459006761624, 1.07590540221728, 
0.976649538677527), `Vila Formosa` = c(1.01682492796219, 1.19637220465518, 
1.14482710265782, 1.16430942850757, 1.00921730819686, 1.09375477196462, 
1.11163326982876), `Vila Guilherme` = c(0.898645775001184, 0.846693379694639, 
1.01472325682071, 1.11690664142431, 0.925497016230146, 1.08301995267962, 
0.701692862671492), `Vila Jacuí` = c(1.15490195998574, 1.15554424920595, 
1.11874706070966, 1.19546108673978, 1.22870210076027, 1.16977805667496, 
1.17500687776346), `Vila Leopoldina` = c(0.790484985457369, 0.633888476621653, 
0.45717457304082, 0.22767829327708, 0.519993057042849, 0.708483153472048, 
0.578396073130169), `Vila Maria` = c(1.18619669533691, 1.15247140706278, 
1.07507492276797, 1.19044028536473, 1.08238947425013, 1.01412464269161, 
1.14468279480406), `Vila Mariana` = c(0.723210054910576, 0.776309409979935, 
0.594141600682363, 0.619788758288394, 0.491133749061542, 0.867171303532789, 
0.758382376069374), `Vila Matilde` = c(0.917867704215493, 0.948525897025594, 
0.854803593885818, 1.06081366765978, 0.78847900275977, 0.944135375464805, 
1.09080068082562), `Vila Medeiros` = c(1.10237290870956, 1.04287180232319, 
1.07169322465507, 1.1004649672251, 0.932557157223619, 1.13715334001706, 
1.06143262607894), `Vila Prudente` = c(0.895512888687605, 1.054203273952, 
0.725182629756266, 1.04852370680383, 0.885055584287415, 1.11350927482752, 
0.981230175053475), `Vila Sônia` = c(0.953374787908098, 1.06816757604767, 
0.72558555133039, 0.968303063813556, 0.725265015127262, 1.18982860289474, 
0.97557297715703), MSP = c(1.04743175054596, 1.04564878847353, 
1.03619246290464, 1.05319039610875, 1.04899586739949, 1.04186635058655, 
1.04870246334299)), row.names = c(NA, 7L), class = "data.frame")
  • The getAnywhere only works if the package where the function exists is loaded. Please edit the question with library(prais) at the beginning of the script. And with the output of dput(head(CMI_lg, 20)).

  • Hi Rui I believe it is in the prais package. At the beginning of the code is already library(prais).

  • I added the output of dput(head(Cmi_lg, 20)) as suggested by @Ruibarradas

1 answer

3


I believe the following answers the question.
Instead of a cycle for, cycle lapply with the names of the answer variables. Then the most difficult is to have column names with special characters. The paste0 serves to have those names between "``". Then the formula of regressions is formed and the model is adjusted. Finally, the second line is extracted and also leaves the cycle with the column name.

library(prais)

pw_resultados <- lapply(names(CMI_lg)[-1], function(a){
  resp <- paste0("`", a, "`")
  fmla <- paste(resp, "Ano", sep = "~")
  fmla <- as.formula(fmla)
  pw <- prais_winsten(fmla, data = CMI_lg)
  cf <- coef(summary(pw))[2, ]
  cbind.data.frame(coluna = a, t(cf))
})
pw_resultados <- do.call(rbind, pw_resultados)

head(pw_resultados)
#             coluna    Estimate Std. Error    t value   Pr(>|t|)
#1         Água Rasa -0.05863596 0.01747921 -3.3546116 0.02022661
#2 Alto de Pinheiros -0.01258421 0.02634637 -0.4776450 0.65305285
#3        Anhanguera -0.05251868 0.01778158 -2.9535432 0.03175499
#4        Aricanduva -0.01188546 0.01479517 -0.8033337 0.45825314
#5       Artur Alvim -0.04345656 0.05855142 -0.7421949 0.49134155
#6        Bela Vista -0.03202344 0.01965581 -1.6292095 0.17860096
  • Did you test and work there? I got errors here, need some package you did not specify? > Linha2 <- lapply(pw_results, Function(res) coef(Summary(res))[2, ]) Error: $ Operator is invalid for Atomic Vectors > linhas2 <- do.call(rbind.data.frame, Linha2) Error in do.call(rbind.data.frame, Linha2) : object 'Linha2' not found > lines2 <- cbind.data.frame(column = Names(Cmi_lg)[-1], lines2)

  • @Luccanielsen See now, tested with the above data and gave no errors.

  • Thank you very much, it worked out! Congratulations for the speed and helpfulness friend

Browser other questions tagged

You are not signed in. Login or sign up in order to post.