R语言 改变y和控制的循环回归模型

plicqrtu  于 2023-01-22  发布在  其他
关注(0)|答案(2)|浏览(153)

我有一个这样的 Dataframe (真实的的 Dataframe 只是有更多的变量):

data<-data.frame(Country=c("USA","USA","USA","USA","India","India","India","India","China","China","China","China"),
               Indicator=rep(c("Population","GDP","Debt","Currency"),times=3),`2011`=rep(c(1,2,3,4),each=3),`2012`=rep(c(4,5,6,7),each=3),`2013`=rep(c(8,9,11,12),each=3))                                                                                                                       

data<-data  %>%
  pivot_longer(
    starts_with("X"),
    names_to = "Year",
    names_transform = list(Year = parse_number)
  ) %>%
  pivot_wider(names_from = Indicator, values_from = value) %>% 
  relocate(Year)

data$y1<-c(1,10,11,3,4,5,2,2,1)
data$y2<-c(1,2,3,4,5,6,6,8,9)
data$y3<-c(10,9,8,7,5,5,11,3,4)
data$y4<-c(1,1,11,3,4,2,2,2,1)
data$y5<-c(5,10,11,3,5,5,5,5,1)

我想每次用一个新的y和x(GDP)与控制变量(在本例中,这些变量是国家、人口、债务和货币)的不同组合来循环线性回归模型,如下所示:

lm_y1_GDP=lm(y1~GDP, data = data)
lm_y1_GDP_year=lm(y1~GDP+year, data = data)
lm_y1_GDP_country=lm(y1~GDP+country, data = data)
...
lm_y5_GDP_country_population_debt_currency=lm(y5~GDP+Country+Population+Debt+Currency, data = data)

并将每个summary(lm_y1), summary(lm_y2),...存储在一个专用的回归结果列表中。理想情况下,我还希望创建虚拟变量来添加到回归列表中,同时添加国家和时间固定效应。提前感谢!

nnsrf1az

nnsrf1az1#

这一个更倾向于Tidyverse,我也留下了Country,据我所知,lm()确实处理因子,尽管在解释结果时需要考虑它。

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

data$Country <- as.factor(data$Country)

lm_out <- map(1:5, ~ combn(c("Year", "Country", "Population", "Debt", "Currency"), .x)) %>% 
  # list of matrices, 1x5, 2x10 .. 4x5, 5x1
  map(~ apply(.x, 2, paste0, collapse = "+")) %>% 
  # formulas without y.~GDP+
  unlist() %>% 
  paste0("GDP+", .) %>%
  # all combinations as data.frame
  expand.grid(y = c("y1", "y2", "y3", "y4", "y5"), terms = .) %>% 
  mutate(frm = paste0(y,"~",terms)) %>% 
  # y       terms            frm
  # 1  y1    GDP+Year    y1~GDP+Year
  # 2  y2    GDP+Year    y2~GDP+Year
  # 3  y3    GDP+Year    y3~GDP+Year
  # 4  y4    GDP+Year    y4~GDP+Year
  # 5  y5    GDP+Year    y5~GDP+Year
  # ...
  pull(frm) %>% 
  set_names() %>% 
  # fit 155 models
  map(~ lm(.x, data = data))

# used formulas can be found from list names:
names(lm_out)[1]
#> [1] "y1~GDP+Year"
summary(lm_out[[1]])
#> 
#> Call:
#> lm(formula = .x, data = data)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -3.3627 -1.6373 -0.2843  2.4118  2.6863 
#> 
#> Coefficients:
#>               Estimate Std. Error t value Pr(>|t|)  
#> (Intercept) -1.604e+04  4.789e+03  -3.350   0.0154 *
#> GDP         -1.677e+00  5.847e-01  -2.867   0.0285 *
#> Year         7.980e+00  2.382e+00   3.351   0.0154 *
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 2.541 on 6 degrees of freedom
#> Multiple R-squared:  0.6541, Adjusted R-squared:  0.5387 
#> F-statistic: 5.672 on 2 and 6 DF,  p-value: 0.0414

# imap to access list names as .y, run tidy and glance over whole list,
# add column with formula
imap_dfr(lm_out, ~ tidy(.x) %>% mutate(formula = .y) )
#> # A tibble: 790 × 6
#>    term          estimate std.error statistic  p.value formula    
#>    <chr>            <dbl>     <dbl>     <dbl>    <dbl> <chr>      
#>  1 (Intercept) -16043.     4789.       -3.35  0.0154   y1~GDP+Year
#>  2 GDP             -1.68      0.585    -2.87  0.0285   y1~GDP+Year
#>  3 Year             7.98      2.38      3.35  0.0154   y1~GDP+Year
#>  4 (Intercept)   8628.     2019.        4.27  0.00524  y2~GDP+Year
#>  5 GDP              1.49      0.246     6.04  0.000933 y2~GDP+Year
#>  6 Year            -4.29      1.00     -4.27  0.00525  y2~GDP+Year
#>  7 (Intercept)   -554.     4645.       -0.119 0.909    y3~GDP+Year
#>  8 GDP             -0.576     0.567    -1.02  0.349    y3~GDP+Year
#>  9 Year             0.280     2.31      0.121 0.907    y3~GDP+Year
#> 10 (Intercept)  -8273.     5882.       -1.41  0.209    y4~GDP+Year
#> # … with 780 more rows

imap_dfr(lm_out, ~ glance(.x) %>% mutate(formula = .y) )
#> # A tibble: 155 × 13
#>    r.squared adj.r.squ…¹ sigma stati…² p.value    df  logLik   AIC   BIC devia…³
#>        <dbl>       <dbl> <dbl>   <dbl>   <dbl> <dbl>   <dbl> <dbl> <dbl>   <dbl>
#>  1     0.654      0.539  2.54    5.67  4.14e-2     2 -19.3    46.7  47.5  38.7  
#>  2     0.879      0.839  1.07   21.8   1.77e-3     2 -11.6    31.1  31.9   6.89 
#>  3     0.420      0.227  2.46    2.18  1.95e-1     2 -19.1    46.1  46.9  36.4  
#>  4     0.269      0.0257 3.12    1.11  3.90e-1     2 -21.2    50.4  51.2  58.5  
#>  5     0.548      0.397  2.43    3.64  9.23e-2     2 -18.9    45.9  46.6  35.4  
#>  6     0.578      0.325  3.07    2.29  1.96e-1     3 -20.2    50.5  51.4  47.2  
#>  7     0.989      0.982  0.356 148.    2.65e-5     3  -0.824  11.6  12.6   0.633
#>  8     0.611      0.378  2.21    2.62  1.63e-1     3 -17.3    44.5  45.5  24.4  
#>  9     0.256     -0.191  3.45    0.573 6.57e-1     3 -21.3    52.5  53.5  59.5  
#> 10     0.580      0.328  2.56    2.30  1.95e-1     3 -18.6    47.2  48.2  32.9  
#> # … with 145 more rows, 3 more variables: df.residual <int>, nobs <int>,
#> #   formula <chr>, and abbreviated variable names ¹​adj.r.squared, ²​statistic,
#> #   ³​deviance

输入:

data<-data.frame(Country=c("USA","USA","USA","USA","India","India","India","India","China","China","China","China"),
                 Indicator=rep(c("Population","GDP","Debt","Currency"),times=3),`2011`=rep(c(1,2,3,4),each=3),`2012`=rep(c(4,5,6,7),each=3),`2013`=rep(c(8,9,11,12),each=3))                                                                                                                       

data<-data  %>%
  pivot_longer(
    starts_with("X"),
    names_to = "Year",
    names_transform = list(Year = readr::parse_number)
  ) %>%
  pivot_wider(names_from = Indicator, values_from = value) %>% 
  relocate(Year)

data$y1<-c(1,10,11,3,4,5,2,2,1)
data$y2<-c(1,2,3,4,5,6,6,8,9)
data$y3<-c(10,9,8,7,5,5,11,3,4)
data$y4<-c(1,1,11,3,4,2,2,2,1)
data$y5<-c(5,10,11,3,5,5,5,5,1)

创建于2023年1月20日,使用reprex v2.0.2

jjhzyzn0

jjhzyzn02#

像这样的例子怎么样?我在do_mods()下面做的函数有几个参数:

  • xvars是x变量名的字符向量。
  • yvars是y变量名称的字符向量。
  • other_ctrls是将始终存在于模型中的其它控制变量的可选字符向量。
  • data是 Dataframe 。

该函数为yvars的每个不同值生成xvars的所有可能组合。输出是一个列表,其中列表的每个元素对应于一个不同的因变量。返回的每个列表元素本身是该因变量的所有模型摘要的列表。

library(dplyr)
library(tidyr)  
library(readr)
data<-data.frame(Country=c("USA","USA","USA","USA","India","India","India","India","China","China","China","China"),
                 Indicator=rep(c("Population","GDP","Debt","Currency"),times=3),`2011`=rep(c(1,2,3,4),each=3),`2012`=rep(c(4,5,6,7),each=3),`2013`=rep(c(8,9,11,12),each=3))                                                                                                                       

data<-data  %>%
  pivot_longer(
    starts_with("X"),
    names_to = "Year",
    names_transform = list(Year = parse_number)
  ) %>%
  pivot_wider(names_from = Indicator, values_from = value) %>% 
  relocate(Year)

data$y1<-c(1,10,11,3,4,5,2,2,1)
data$y2<-c(1,2,3,4,5,6,6,8,9)
data$y3<-c(10,9,8,7,5,5,11,3,4)
data$y4<-c(1,1,11,3,4,2,2,2,1)
data$y5<-c(5,10,11,3,5,5,5,5,1)

xvars <- c("Population", "GDP", "Debt", "Currency")
yvars <- c("y1", "y2", "y3", "y4", "y5")

do_mods <- function(xvars, yvars, other_ctrls = NULL, data, ...){

xspec <- lapply(seq_along(xvars), function(x)combn(xvars, x))
forms <- sapply(yvars, function(y){
  c(unlist(sapply(xspec, function(x)apply(x, 2, function(z)
    paste0(y, " ~ ", paste(c(z, other_ctrls), collapse=" + "))))))
})       

out <- lapply(1:ncol(forms), function(i){
  lapply(1:length(forms[,i]), function(j){
    summary(lm(forms[j,i], data))
  })
})
names(out) <- colnames(forms)
out
}

res <- do_mods(xvars, yvars, "Country", data)
names(res)
#> [1] "y1" "y2" "y3" "y4" "y5"

res[[1]]
#> [[1]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>       1       2       3       4       5       6       7       8       9 
#> -4.8564  2.8144  2.0420  0.4770  0.1477 -0.6247  1.9580  0.6287 -2.5867 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)   -1.2873     2.8756  -0.448   0.6731  
#> Population     0.4431     0.3394   1.305   0.2486  
#> CountryIndia   2.9241     2.5500   1.147   0.3034  
#> CountryUSA     6.7005     2.6316   2.546   0.0515 .
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.074 on 5 degrees of freedom
#> Multiple R-squared:  0.5783, Adjusted R-squared:  0.3252 
#> F-statistic: 2.285 on 3 and 5 DF,  p-value: 0.1963
#> 
#> 
#> [[2]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>       1       2       3       4       5       6       7       8       9 
#> -4.8564  2.8144  2.0420  0.4770  0.1477 -0.6247  1.9580  0.6287 -2.5867 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)   -1.7304     3.1497  -0.549   0.6064  
#> GDP            0.4431     0.3394   1.305   0.2486  
#> CountryIndia   3.3672     2.6316   1.280   0.2569  
#> CountryUSA     7.1436     2.7528   2.595   0.0485 *
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.074 on 5 degrees of freedom
#> Multiple R-squared:  0.5783, Adjusted R-squared:  0.3252 
#> F-statistic: 2.285 on 3 and 5 DF,  p-value: 0.1963
#> 
#> 
#> [[3]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>       1       2       3       4       5       6       7       8       9 
#> -4.9506  2.8049  2.1457  0.5210  0.2765 -0.7975  1.8543  0.6099 -2.4642 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)   -1.5136     3.0724  -0.493   0.6431  
#> Debt           0.4148     0.3261   1.272   0.2593  
#> CountryIndia   2.7481     2.5467   1.079   0.3298  
#> CountryUSA     7.0494     2.7497   2.564   0.0504 .
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.093 on 5 degrees of freedom
#> Multiple R-squared:  0.5728, Adjusted R-squared:  0.3165 
#> F-statistic: 2.235 on 3 and 5 DF,  p-value: 0.2021
#> 
#> 
#> [[4]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>       1       2       3       4       5       6       7       8       9 
#> -4.9506  2.8049  2.1457  0.5210  0.2765 -0.7975  1.8543  0.6099 -2.4642 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)   -1.5136     3.0724  -0.493   0.6431  
#> Currency       0.4148     0.3261   1.272   0.2593  
#> CountryIndia   2.7481     2.5467   1.079   0.3298  
#> CountryUSA     6.6346     2.6379   2.515   0.0535 .
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.093 on 5 degrees of freedom
#> Multiple R-squared:  0.5728, Adjusted R-squared:  0.3165 
#> F-statistic: 2.235 on 3 and 5 DF,  p-value: 0.2021
#> 
#> 
#> [[5]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>       1       2       3       4       5       6       7       8       9 
#> -4.8564  2.8144  2.0420  0.4770  0.1477 -0.6247  1.9580  0.6287 -2.5867 
#> 
#> Coefficients: (1 not defined because of singularities)
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)     5.413      2.305   2.349   0.0657 .
#> Population      7.144      2.753   2.595   0.0485 *
#> GDP            -6.701      2.632  -2.546   0.0515 .
#> CountryIndia   -3.776      2.532  -1.491   0.1961  
#> CountryUSA         NA         NA      NA       NA  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.074 on 5 degrees of freedom
#> Multiple R-squared:  0.5783, Adjusted R-squared:  0.3252 
#> F-statistic: 2.285 on 3 and 5 DF,  p-value: 0.1963
#> 
#> 
#> [[6]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>          1          2          3          4          5          6          7 
#> -4.671e+00  2.833e+00  1.838e+00  2.480e-01 -2.480e-01 -5.773e-15  2.162e+00 
#>          8          9 
#>  6.658e-01 -2.827e+00 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)
#> (Intercept)   -0.4151     4.6421  -0.089    0.933
#> Population     1.7412     5.0357   0.346    0.747
#> Debt          -1.2426     4.8067  -0.259    0.809
#> CountryIndia   3.4124     3.4003   1.004    0.372
#> CountryUSA     5.5876     5.2008   1.074    0.343
#> 
#> Residual standard error: 3.408 on 4 degrees of freedom
#> Multiple R-squared:  0.5852, Adjusted R-squared:  0.1704 
#> F-statistic: 1.411 on 4 and 4 DF,  p-value: 0.3734
#> 
#> 
#> [[7]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>          1          2          3          4          5          6          7 
#> -4.671e+00  2.833e+00  1.838e+00  2.480e-01 -2.480e-01 -1.288e-14  2.162e+00 
#>          8          9 
#>  6.658e-01 -2.827e+00 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)   -0.4151     4.6421  -0.089   0.9330  
#> Population     1.7412     5.0357   0.346   0.7469  
#> Currency      -1.2426     4.8067  -0.259   0.8088  
#> CountryIndia   3.4124     3.4003   1.004   0.3724  
#> CountryUSA     6.8302     2.9607   2.307   0.0823 .
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.408 on 4 degrees of freedom
#> Multiple R-squared:  0.5852, Adjusted R-squared:  0.1704 
#> F-statistic: 1.411 on 4 and 4 DF,  p-value: 0.3734
#> 
#> 
#> [[8]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>          1          2          3          4          5          6          7 
#> -4.671e+00  2.833e+00  1.838e+00  2.480e-01 -2.480e-01 -1.110e-14  2.162e+00 
#>          8          9 
#>  6.658e-01 -2.827e+00 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)    -2.156      3.862  -0.558   0.6063  
#> GDP             1.741      5.036   0.346   0.7469  
#> Debt           -1.243      4.807  -0.259   0.8088  
#> CountryIndia    5.154      7.501   0.687   0.5298  
#> CountryUSA      7.329      3.135   2.338   0.0796 .
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.408 on 4 degrees of freedom
#> Multiple R-squared:  0.5852, Adjusted R-squared:  0.1704 
#> F-statistic: 1.411 on 4 and 4 DF,  p-value: 0.3734
#> 
#> 
#> [[9]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>          1          2          3          4          5          6          7 
#> -4.671e+00  2.833e+00  1.838e+00  2.480e-01 -2.480e-01 -9.992e-15  2.162e+00 
#>          8          9 
#>  6.658e-01 -2.827e+00 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)
#> (Intercept)    -2.156      3.862  -0.558    0.606
#> GDP             1.741      5.036   0.346    0.747
#> Currency       -1.243      4.807  -0.259    0.809
#> CountryIndia    5.154      7.501   0.687    0.530
#> CountryUSA      8.571      6.310   1.358    0.246
#> 
#> Residual standard error: 3.408 on 4 degrees of freedom
#> Multiple R-squared:  0.5852, Adjusted R-squared:  0.1704 
#> F-statistic: 1.411 on 4 and 4 DF,  p-value: 0.3734
#> 
#> 
#> [[10]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>       1       2       3       4       5       6       7       8       9 
#> -4.9506  2.8049  2.1457  0.5210  0.2765 -0.7975  1.8543  0.6099 -2.4642 
#> 
#> Coefficients: (1 not defined because of singularities)
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)    -1.514      3.072  -0.493   0.6431  
#> Debt           -6.635      2.638  -2.515   0.0535 .
#> Currency        7.049      2.750   2.564   0.0504 .
#> CountryIndia    2.748      2.547   1.079   0.3298  
#> CountryUSA         NA         NA      NA       NA  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.093 on 5 degrees of freedom
#> Multiple R-squared:  0.5728, Adjusted R-squared:  0.3165 
#> F-statistic: 2.235 on 3 and 5 DF,  p-value: 0.2021
#> 
#> 
#> [[11]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>          1          2          3          4          5          6          7 
#> -4.671e+00  2.833e+00  1.838e+00  2.480e-01 -2.480e-01 -5.329e-15  2.162e+00 
#>          8          9 
#>  6.658e-01 -2.827e+00 
#> 
#> Coefficients: (1 not defined because of singularities)
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)     5.173      2.720   1.902   0.1300  
#> Population      7.329      3.135   2.338   0.0796 .
#> GDP            -5.588      5.201  -1.074   0.3431  
#> Debt           -1.243      4.807  -0.259   0.8088  
#> CountryIndia   -2.175      6.801  -0.320   0.7651  
#> CountryUSA         NA         NA      NA       NA  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.408 on 4 degrees of freedom
#> Multiple R-squared:  0.5852, Adjusted R-squared:  0.1704 
#> F-statistic: 1.411 on 4 and 4 DF,  p-value: 0.3734
#> 
#> 
#> [[12]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>          1          2          3          4          5          6          7 
#> -4.671e+00  2.833e+00  1.838e+00  2.480e-01 -2.480e-01 -1.310e-14  2.162e+00 
#>          8          9 
#>  6.658e-01 -2.827e+00 
#> 
#> Coefficients: (1 not defined because of singularities)
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)     6.415      4.642   1.382   0.2392  
#> Population      8.571      6.310   1.358   0.2459  
#> GDP            -6.830      2.961  -2.307   0.0823 .
#> Currency       -1.243      4.807  -0.259   0.8088  
#> CountryIndia   -3.418      3.132  -1.091   0.3365  
#> CountryUSA         NA         NA      NA       NA  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.408 on 4 degrees of freedom
#> Multiple R-squared:  0.5852, Adjusted R-squared:  0.1704 
#> F-statistic: 1.411 on 4 and 4 DF,  p-value: 0.3734
#> 
#> 
#> [[13]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>          1          2          3          4          5          6          7 
#> -4.671e+00  2.833e+00  1.838e+00  2.480e-01 -2.480e-01 -1.332e-14  2.162e+00 
#>          8          9 
#>  6.658e-01 -2.827e+00 
#> 
#> Coefficients: (1 not defined because of singularities)
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)   -0.4151     4.6421  -0.089   0.9330  
#> Population     1.7412     5.0357   0.346   0.7469  
#> Debt          -6.8302     2.9607  -2.307   0.0823 .
#> Currency       5.5876     5.2008   1.074   0.3431  
#> CountryIndia   3.4124     3.4003   1.004   0.3724  
#> CountryUSA         NA         NA      NA       NA  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.408 on 4 degrees of freedom
#> Multiple R-squared:  0.5852, Adjusted R-squared:  0.1704 
#> F-statistic: 1.411 on 4 and 4 DF,  p-value: 0.3734
#> 
#> 
#> [[14]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>          1          2          3          4          5          6          7 
#> -4.671e+00  2.833e+00  1.838e+00  2.480e-01 -2.480e-01 -1.110e-14  2.162e+00 
#>          8          9 
#>  6.658e-01 -2.827e+00 
#> 
#> Coefficients: (1 not defined because of singularities)
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)    -2.156      3.862  -0.558   0.6063  
#> GDP             1.741      5.036   0.346   0.7469  
#> Debt           -8.571      6.310  -1.358   0.2459  
#> Currency        7.329      3.135   2.338   0.0796 .
#> CountryIndia    5.154      7.501   0.687   0.5298  
#> CountryUSA         NA         NA      NA       NA  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.408 on 4 degrees of freedom
#> Multiple R-squared:  0.5852, Adjusted R-squared:  0.1704 
#> F-statistic: 1.411 on 4 and 4 DF,  p-value: 0.3734
#> 
#> 
#> [[15]]
#> 
#> Call:
#> lm(formula = forms[j, i], data = data)
#> 
#> Residuals:
#>          1          2          3          4          5          6          7 
#> -4.671e+00  2.833e+00  1.838e+00  2.480e-01 -2.480e-01 -1.310e-14  2.162e+00 
#>          8          9 
#>  6.658e-01 -2.827e+00 
#> 
#> Coefficients: (2 not defined because of singularities)
#>              Estimate Std. Error t value Pr(>|t|)
#> (Intercept)     2.997      5.966   0.502    0.642
#> Population      5.154      7.501   0.687    0.530
#> GDP            -3.412      3.400  -1.004    0.372
#> Debt           -3.418      3.132  -1.091    0.336
#> Currency        2.175      6.801   0.320    0.765
#> CountryIndia       NA         NA      NA       NA
#> CountryUSA         NA         NA      NA       NA
#> 
#> Residual standard error: 3.408 on 4 degrees of freedom
#> Multiple R-squared:  0.5852, Adjusted R-squared:  0.1704 
#> F-statistic: 1.411 on 4 and 4 DF,  p-value: 0.3734

reprex package(v2.0.1)于2023年1月20日创建

相关问题