R语言 将lm结果写入表

hmtdttj4  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(120)

我正在执行一系列R中的年度横截面回归,从Excel工作簿中导入数据,其中每张表对应一年,这些从1995年到2018年。我不明白我如何将系数列表及其p值添加到resultados_df中。
脚本如下:

library(lmtest)
library(olsrr)
library(tseries)
library(readxl)
library(openxlsx) 
library(dplyr)

años <- 1995:2018 

resultados <- list()

for (año in años) {
  # Define el nombre del archivo Excel (puedes modificarlo según tus necesidades)
  archivo_excel <- "C:/Users/Kevin/Desktop/Bases/base_Regresiones.xlsx"
  
  # Importa los datos desde la hoja correspondiente al año
  datos <- read_excel(archivo_excel, sheet = as.character(año))

  # Realiza la regresión con tus datos. Por ejemplo, aquí se asume que tienes dos columnas: 'variable_independiente' y 'variable_dependiente'.
  modelo <- lm(y ~ ., data = datos)
  
  # Calcula los estadísticos requeridos
  r_squared <- summary(modelo)$r.squared
  f_statistic <- summary(modelo)$fstatistic[1]
  ssr <- deviance(modelo)
  pvalues <- summary(modelo)$coefficients[, "Pr(>|t|)"]
  jb_test <- jarque.bera.test(residuals(modelo))
  bg_test <- bgtest(modelo)
  
  # Almacena los resultados en la lista, incluyendo el R-squared y el estadístico F
  resultados[[as.character(año)]] <- list(
    Año = año,
    R_squared = r_squared,
    F_statistic = f_statistic,
    u_squad = ssr,
    Coeficientes = coef(modelo),
    Pvalues = pvalues,
    JBtest = jb_test$p.value,
    BGtest = bg_test$p.value,
    BPtest = bp_test$p.value
  )
}

# Crear un DataFrame a partir de la lista de resultados
resultados_df <- do.call(rbind, resultados)

字符串
我得到了这些结果

Año  R_squared F_statistic u_squad  JBtest   
1995 0.9480865 148.5375    10.46748 0.4176042
1996 0.9485575 149.972     10.31898 0.2332913
1997 0.9462563 142.0284    10.8743  0.3334412
1998 0.9464819 143.84      10.8509  0.5535448
1999 0.9475053 145.5997    10.57655 0.4754198
2000 0.9472141 144.7519    10.81674 0.3440291
2001 0.9504769 156.0998    10.27286 0.8708391
2002 0.9494986 151.6648    10.43004 0.8408953
2003 0.9502323 155.2925    10.36924 0.6420716
2004 0.9501257 154.9432    10.38435 0.8029328
2005 0.9498327 153.991     10.4385  0.2381718
2006 0.9518624 160.8269    10.04476 0.3467262
2007 0.9513862 159.1718    10.17184 0.3430534
2008 0.9468287 144.8315    10.96748 0.4548003
2009 0.9525406 163.2413    9.350981 0.5795036
2010 0.948046  148.4155    10.10924 0.6508358
2011 0.9510188 157.9168    9.487844 0.5200191
2012 0.948731  150.507     9.751045 0.5669693
2013 0.9453135 140.5932    10.37591 0.4488269
2014 0.9465016 143.8963    10.07656 0.4465016
2015 0.9469356 145.1395    10.06823 0.2957086
2016 0.9479821 148.223     9.874893 0.2328414
2017 0.9452753 140.4895    10.42455 0.1856073
2018 0.9451515 140.154     10.45936 0.2261362


我希望能得到以下

Año  R_squared F_statistic u_squad  JBtest   constant x1       x2...    x2018 pvalue_x1... pvalue_x2018
1995 0.9480865 148.5375    10.46748 0.4176042 0.344558 0.245688 0.242411        0.03
1996 0.9485575 149.972     10.31898 0.2332913 0.545682 0.833226 0.643456        0.02
1997 0.9462563 142.0284    10.8743  0.3334412 0.788953 0.412100 0.142731        0.11
1998 0.9464819 143.84      10.8509  0.5535448
1999 0.9475053 145.5997    10.57655 0.4754198
2000 0.9472141 144.7519    10.81674 0.3440291
2001 0.9504769 156.0998    10.27286 0.8708391
2002 0.9494986 151.6648    10.43004 0.8408953
2003 0.9502323 155.2925    10.36924 0.6420716
2004 0.9501257 154.9432    10.38435 0.8029328
2005 0.9498327 153.991     10.4385  0.2381718
2006 0.9518624 160.8269    10.04476 0.3467262
2007 0.9513862 159.1718    10.17184 0.3430534
2008 0.9468287 144.8315    10.96748 0.4548003
2009 0.9525406 163.2413    9.350981 0.5795036
2010 0.948046  148.4155    10.10924 0.6508358
2011 0.9510188 157.9168    9.487844 0.5200191
2012 0.948731  150.507     9.751045 0.5669693
2013 0.9453135 140.5932    10.37591 0.4488269
2014 0.9465016 143.8963    10.07656 0.4465016
2015 0.9469356 145.1395    10.06823 0.2957086
2016 0.9479821 148.223     9.874893 0.2328414
2017 0.9452753 140.4895    10.42455 0.1856073
2018 0.9451515 140.154     10.45936 0.2261362

huwehgph

huwehgph1#

我会写一个小的“整理”函数来收集所有的结果。在这种情况下,函数my_tidy()接受一个模型作为输入,并输出一个 Dataframe ,在一行中包含所有你想要的东西。然后你可以使用bind_rows()把所有东西放在一起。下面是一个mtcars数据的例子。

library(dplyr)
data(mtcars)
mtcars$cyl <- factor(mtcars$cyl, levels=c(4,6,8), 
                     labels=c("4 cyl", "6 cyl", "8 cyl"))

my_tidy <- function(model, ...){
  s <- summary(model)
  pvalues <- s$coefficients[,4]
  jb_test <- DescTools::JarqueBeraTest(residuals(model))
  bg_test <- lmtest::bgtest(model)
  bp_test <- lmtest::bptest(model)
  coefs <- do.call(data.frame, as.list(coef(model)))
  pvals <- do.call(data.frame, as.list(pvalues))
  names(pvals) <- paste0("p.", names(pvals))
  bind_cols(
    tibble::tibble(
      R_squared = s$r.squared,
      F_statistic = s$fstatistic[1],
      u_squad = deviance(model),
      JBtest = jb_test$p.value,
      BGtest = bg_test$p.value, 
      BPtest = bp_test$p.value), 
    coefs, 
    pvals)
}
res <- list()
for(x in levels(mtcars$cyl)){
  model <- lm(qsec ~ hp + wt + drat + disp, 
              data = filter(mtcars, cyl == x))
  res[[x]] <- my_tidy(model)
}

bind_rows(res, .id="cyl")
#> # A tibble: 3 × 17
#>   cyl   R_squared F_statistic u_squad JBtest  BGtest BPtest X.Intercept.
#>   <chr>     <dbl>       <dbl>   <dbl>  <dbl>   <dbl>  <dbl>        <dbl>
#> 1 4 cyl     0.596        2.22  11.4    0.637 0.125    0.157         18.5
#> 2 6 cyl     0.989       43.1    0.200  0.691 0.00886  0.152         12.0
#> 3 8 cyl     0.930       30.0    1.30   0.213 0.518    0.311         19.3
#> # ℹ 9 more variables: hp <dbl>, wt <dbl>, drat <dbl>, disp <dbl>,
#> #   p.X.Intercept. <dbl>, p.hp <dbl>, p.wt <dbl>, p.drat <dbl>, p.disp <dbl>

创建于2023-10-31使用reprex v2.0.2

相关问题