【问题标题】:Multiple covariates for multiple linear models多个线性模型的多个协变量
【发布时间】:2021-03-06 20:53:33
【问题描述】:

我正在尝试使用相同的y 变量拟合多个线性模型,但使用不同的x 协变量序列,因此我应该只获得适合yyear + pland_ 的线性模型。

这是我尝试过的一个示例:

lm.test <- list(formula(I(test.1.l1)[1:3]), formula(I(test.1.l1)[c(1, 2, 4)]),   
formula(I(test.1.l1)[c(1, 2, 5)]),  
formula(I(test.1.l1)[c(1, 2, 6)]), 
formula(I(test.1.l1)[c(1, 2, 7)]),  
formula(I(test.1.l1)[c(1, 2, 8)]),   
formula(I(test.1.l1)[c(1, 2, 9)]),   
formula(I(test.1.l1)[c(1, 2, 10)]), 
formula(I(test.1.l1)[c(1, 2, 11)]),   
formula(I(test.1.l1)[c(1, 2, 12)]), 
formula(I(test.1.l1)[c(1, 2, 13)]), 
formula(I(test.1.l1)[c(1, 2, 14)]),   
formula(I(test.1.l1)[c(1, 2, 15)]), 
formula(I(test.1.l1)[c(1, 2, 16)]),   
formula(I(test.1.l1)[c(1, 2, 17)]), 
formula(I(test.1.l1)[c(1, 2, 18)]))

layer ~ year + pland_00_water

[[2]]
layer ~ year + pland_01_evergreen_needleleaf

[[3]]
layer ~ year + pland_02_evergreen_broadleaf

[[4]]
layer ~ year + pland_03_deciduous_needleleaf

[[5]]
layer ~ year + pland_04_deciduous_broadleaf

[[6]]
layer ~ year + pland_05_mixed_forest

[[7]]
layer ~ year + pland_06_closed_shrubland

[[8]]
layer ~ year + pland_07_open_shrubland

[[9]]
layer ~ year + pland_08_woody_savanna

[[10]]
layer ~ year + pland_09_savanna

[[11]]
layer ~ year + pland_10_grassland

[[12]]
layer ~ year + pland_11_wetland

[[13]]
layer ~ year + pland_12_cropland

[[14]]
layer ~ year + pland_13_urban

[[15]]
layer ~ year + pland_14_mosiac

[[16]]
layer ~ year + pland_15_barren

然后通过lapply运行线性模型:

lapply(test.1.l1, lm(covariates))

不过,我得到了错误:

formula.default(object, env = baseenv()) 中的错误:公式无效

示例代码:

structure(list(layer = c(0.0246770934766904, 0.0242527868989167, 
0.0224233190566232, 0.0216524802418302, 0.022118695762337, 0.0209053824836735, 
0.0211326999515466, 0.0217314614023917, 0.0226449195846084, 0.0264381358128127
), year = 2010:2019, pland_00_water = c(0.720970123537124, 0.869977032914356, 
0.914640602650167, 0.966042210923708, 1.02071658077272, 1.03554026555572, 
1.01012823449914, 0.997999765131228, 0.946598156857687, 0.870939609848317
), pland_01_evergreen_needleleaf = c(0.061219892999948, 0.0656477468961707, 
0.0660327776697553, 0.067187869990509, 0.0704606315659779, 0.0766211239433312, 
0.0835516778678536, 0.0700756007923933, 0.0841292240282305, 0.0793163393584232
), pland_02_evergreen_broadleaf = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

【问题讨论】:

    标签: r linear-regression


    【解决方案1】:
    lapply(
      c("pland_00_water", "pland_01_evergreen_needleleaf", "pland_02_evergreen_broadleaf"),
      function(x) lm(as.formula(paste0("layer ~ year + ", x)), test.1.l1)
    )
    

    给予

    [[1]]
    
    Call:
    lm(formula = as.formula(paste0("layer ~ year + ", x)), data = test.1.l1)
    
    Coefficients:
       (Intercept)            year  pland_00_water  
        -0.4907930       0.0002635      -0.0184145  
    
    
    [[2]]
    
    Call:
    lm(formula = as.formula(paste0("layer ~ year + ", x)), data = test.1.l1)
    
    Coefficients:
                      (Intercept)                           year  pland_01_evergreen_needleleaf  
                       -0.4924188                      0.0002601                     -0.1211730  
    
    
    [[3]]
    
    Call:
    lm(formula = as.formula(paste0("layer ~ year + ", x)), data = test.1.l1)
    
    Coefficients:
                     (Intercept)                          year  pland_02_evergreen_broadleaf  
                       4.279e-02                    -9.925e-06                            NA 
    

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多