将循环结果带到数据帧

时间:2019-12-16 03:05:41

标签: r

我正在运行以下代码:

#Importar los datos
boyanza <- read_excel("Desktop/Tesina - Boyanza del ITBIS/Data/Data ITBIS:PIB Trimestral.xlsx")
boyanza <- boyanza%>%select(Sector,Fecha,lpib,lrecaudacion)

#Obtener los nombres de los sectores para separarlos al momento de estimar
serie_unica <- unique(boyanza$Sector)

#Estimacion por cada sector
Modelo_MCO <- function(i) {

Sector_elegido <- boyanza %>% 
    filter(Sector == serie_unica[i]) #Estimar cada serie por separado

  MCO <- lm(Sector_elegido$lrecaudacion ~ Sector_elegido$lpib) #Ejecutar el modelo MCO

  #Probar raiz unitaria / cointegracion

}

lapply(1:length(serie_unica), Modelo_MCO)

我的问题是结果以不希望的方式出现。我想在数据帧中使用其Sector名称(在执行for循环时我什至不知道哪个结果属于谁)和系数来获取每个结果。另外,我计划继续将这些内容作为单元根测试添加到for循环中,并将其他内容也显示在所需的数据框中。

我想要的输出将是一个具有6个变量的数据帧:SectorInterceptCoefficientIntercept p-valueCoefficient p-valueUnit root test for循环中的每个变量。

这是我的数据的子集:

structure(list(Sector = c("Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Agropecuaria", "Agropecuaria", "Agropecuaria", 
"Agropecuaria", "Manufactura", "Manufactura", "Manufactura", 
"Manufactura", "Manufactura", "Manufactura", "Manufactura", "Manufactura", 
"Manufactura", "Manufactura", "Manufactura", "Manufactura", "Manufactura", 
"Manufactura", "Manufactura", "Manufactura", "Manufactura", "Manufactura", 
"Manufactura", "Manufactura", "Manufactura", "Manufactura", "Manufactura", 
"Manufactura", "Manufactura", "Manufactura", "Manufactura", "Manufactura", 
"Manufactura", "Manufactura", "Manufactura", "Manufactura", "Manufactura", 
"Manufactura", "Manufactura", "Manufactura", "Manufactura", "Manufactura", 
"Manufactura", "Manufactura", "Manufactura", "Manufactura", "Manufactura", 
"Manufactura", "Manufactura", "Manufactura", "Manufactura", "Manufactura"
), Fecha = c("2007 Q1", "2007 Q2", "2007 Q3", "2007 Q4", "2008 Q1", 
"2008 Q2", "2008 Q3", "2008 Q4", "2009 Q1", "2009 Q2", "2009 Q3", 
"2009 Q4", "2010 Q1", "2010 Q2", "2010 Q3", "2010 Q4", "2011 Q1", 
"2011 Q2", "2011 Q3", "2011 Q4", "2012 Q1", "2012 Q2", "2012 Q3", 
"2012 Q4", "2013 Q1", "2013 Q2", "2013 Q3", "2013 Q4", "2014 Q1", 
"2014 Q2", "2014 Q3", "2014 Q4", "2015 Q1", "2015 Q2", "2015 Q3", 
"2015 Q4", "2016 Q1", "2016 Q2", "2016 Q3", "2016 Q4", "2017 Q1", 
"2017 Q2", "2017 Q3", "2017 Q4", "2018 Q1", "2018 Q2", "2018 Q3", 
"2018 Q4", "2007 Q1", "2007 Q2", "2007 Q3", "2007 Q4", "2008 Q1", 
"2008 Q2", "2008 Q3", "2008 Q4", "2009 Q1", "2009 Q2", "2009 Q3", 
"2009 Q4", "2010 Q1", "2010 Q2", "2010 Q3", "2010 Q4", "2011 Q1", 
"2011 Q2", "2011 Q3", "2011 Q4", "2012 Q1", "2012 Q2", "2012 Q3", 
"2012 Q4", "2013 Q1", "2013 Q2", "2013 Q3", "2013 Q4", "2014 Q1", 
"2014 Q2", "2014 Q3", "2014 Q4", "2015 Q1", "2015 Q2", "2015 Q3", 
"2015 Q4", "2016 Q1", "2016 Q2", "2016 Q3", "2016 Q4", "2017 Q1", 
"2017 Q2", "2017 Q3", "2017 Q4", "2018 Q1", "2018 Q2", "2018 Q3", 
"2018 Q4"), lpib = c(23.9208192513736, 23.9547793577876, 23.7113615363005, 
23.9551561816675, 23.9791871131267, 24.0609327260839, 23.8522054499151, 
24.0672619240971, 24.0439628065799, 24.08862751044, 23.8191462398426, 
24.0489012378328, 24.1342125700793, 24.2363749536441, 23.9301951603667, 
24.1758581146702, 24.1553163277134, 24.269228441101, 24.0423988905311, 
24.1680724981159, 24.2146417516396, 24.3370349486954, 23.9721365153744, 
24.2250024811208, 24.2581482159441, 24.4043429274765, 24.0991585782936, 
24.299670204971, 24.3285037210178, 24.4382015210812, 24.1932843840038, 
24.4599687669099, 24.4790995700253, 24.5509178674757, 24.338977945028, 
24.6336525132489, 24.5911964739263, 24.695033731291, 24.4769376944737, 
24.6039150575164, 24.5981318722398, 24.755039330747, 24.5199473144748, 
24.7136028738888, 24.6989671055154, 24.831274745995, 24.6085743595799, 
24.7352657879472, 24.7319647582932, 24.7798391526453, 24.8149774885031, 
24.8821010050744, 24.8558392232244, 24.9701910228009, 24.9503173375001, 
24.9501614508357, 24.8522534989671, 24.9379219931171, 24.9201527857609, 
24.9805072834487, 24.9724691695314, 25.0618973035633, 25.0438813138428, 
25.116853540493, 25.0816465995181, 25.1474051066843, 25.1485676841393, 
25.2008558670854, 25.1590422450739, 25.2244062436121, 25.2046104101159, 
25.2755162410203, 25.1888680726842, 25.2901601006579, 25.2897662868454, 
25.3564698732644, 25.2862362685789, 25.38353198895, 25.400731741936, 
25.4816147161362, 25.4105749125337, 25.4835592423713, 25.5034625326161, 
25.5662013831766, 25.4657880856982, 25.5766373773005, 25.5478739074007, 
25.6314476014617, 25.5560004217414, 25.6215416376154, 25.6054785357995, 
25.6964258190478, 25.6534089669307, 25.756389929803, 25.7214097882784, 
25.7909791673757), lrecaudacion = c(17.3165532745961, 17.5254173792703, 
17.7467477823162, 17.5808879830509, 17.6415398502254, 17.6764507731631, 
17.6380463968909, 17.4512529136651, 17.406225763291, 17.6530255112174, 
17.6553928856147, 17.448877302148, 17.5206625731235, 17.6725343233053, 
17.766425875705, 18.1696094586812, 17.777201932113, 17.5443784626572, 
17.5096720403233, 17.6231117987477, 17.5944826852452, 17.8676508866123, 
17.7121597816971, 17.6023653836924, 17.5991975078308, 17.694213627511, 
17.8822726638377, 17.9555449536108, 17.8783488578582, 18.2514969193021, 
18.2820155091685, 18.5125024965362, 18.2700852192986, 18.1048409050765, 
18.1106238822944, 18.1115957383498, 18.1996426513536, 18.1442227936413, 
18.1443607802437, 18.3144370714263, 18.2171204831367, 18.2790874105239, 
18.1391719826773, 18.1861918357785, 18.6487752293812, 18.7988843607231, 
18.672835240355, 18.938792447297, 21.5540727517896, 21.670861418649, 
21.6841512776958, 21.7581534954001, 21.7331525529579, 21.9123919728989, 
21.9097913180685, 21.8399537723418, 21.9227967908448, 21.9822891684126, 
22.0428459784791, 22.060711282171, 22.0879051223452, 22.1220660921817, 
22.0833817549598, 21.9323510784567, 21.8631500561848, 21.8018369475668, 
21.7593763504405, 21.8867036097524, 22.014404323807, 22.0265238580423, 
22.1921771821384, 22.2188301834985, 22.3233094942489, 22.3608952722807, 
22.4276299180547, 22.5011424750138, 22.5621408760058, 22.4944203390804, 
22.5229715486123, 22.622312019965, 22.6507157318225, 22.6106757254605, 
22.6181855728448, 22.6297467977415, 22.6458690108391, 22.7187545265385, 
22.6957095238767, 22.706132832817, 22.6551536149017, 22.5499230156297, 
22.6232920327693, 22.5587827151805, 22.6575653854989, 22.618727517277, 
22.6249872062644, 22.6243261773279)), row.names = c(NA, -96L), class = c("tbl_df", 
"tbl", "data.frame"))

3 个答案:

答案 0 :(得分:0)

这将帮助您入门,您快要准备好了。函数的最后一行将返回模型系数(减去单位根检验)。您可以使用setNames将名称添加到列表中。

library(dplyr)

#Estimacion por cada sector
Modelo_MCO <- function(i) {

  Sector_elegido <- boyanza %>% 
    filter(Sector == serie_unica[i]) #Estimar cada serie por separado

  MCO <- lm(Sector_elegido$lrecaudacion ~ Sector_elegido$lpib) #Ejecutar el modelo MCO

  #Probar raiz unitaria / cointegracion
  coef(summary(MCO)) # here
}

lapply(1:length(serie_unica), Modelo_MCO) %>%
  setNames(serie_unica) # here

$Agropecuaria
                     Estimate Std. Error   t value     Pr(>|t|)
(Intercept)         -8.790522  2.9215845 -3.008820 4.244507e-03
Sector_elegido$lpib  1.100120  0.1202964  9.145078 6.449666e-12

$Manufactura
                     Estimate Std. Error   t value     Pr(>|t|)
(Intercept)         -5.980070 1.88292993 -3.175939 2.666331e-03
Sector_elegido$lpib  1.117588 0.07453399 14.994340 2.501412e-19

答案 1 :(得分:0)

您可以创建一个函数,并从数据框中的每个组收集所需的所有值。

fun <T> Context.arrayAdapter(objects: List<T>): ArrayAdapter<T> {
    return ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, objects)
}

然后将功能应用于每个组

def getFood(food_name):
    fooddict = Food.retreiveFoodDict()
        for key in list(fooddict):
           if key.__eq__(food_name):
               return fooddict[key]

您可以在函数中更改数据框的列名,并添加返回它所需的所有必需参数。

答案 2 :(得分:0)

它需要一些软件包,但是broom和朋友可以非常强大:

library(broom)
library(purrr)
library(tidyr)
library(dplyr)

tmp <- boyanza %>% 
    group_by(Sector) %>% 
    nest() %>%
    mutate(models = map(data, function(x) lm(lrecaudacion ~ lpib, data = x))) %>%
    mutate(tidy_sum = map(models, function(x) tidy(x)))

然后您可以通过tmp$tidy_sum[[1]]等访问各个摘要。