str2lang(x) 中的错误:<text>:2:0:输入意外结束
Error in str2lang(x) : <text>:2:0: unexpected end of input
我正在尝试为线性回归模型遍历多个 Y 和 X 变量。
想法是在每个单独的 X 变量上循环 Y1 变量,然后在每个 X 变量上循环 Y2 等等...
类似于:Y1 ... Yn ~ X .. Xn
代码看起来像这样:
t1 <- lapply(
c("pland_00_water", "pland_01_evergreen_needleleaf", "pland_02_evergreen_broadleaf","pland_03_deciduous_needleleaf", "pland_04_deciduous_broadleaf" , "pland_05_mixed_forest",
"pland_06_closed_shrubland" , "pland_07_open_shrubland" , "pland_08_woody_savanna" , "pland_09_savanna" ,
"pland_10_grassland" , "pland_11_wetland" , "pland_12_cropland" , "pland_13_urban" ,
"pland_14_mosiac" , "pland_15_barren"),
function(x) summary(lm(as.formula(string_formula, x), test.all))
)
其中字符串公式是这个代码:
string_formula <- sprintf("cbind(%s) ~ year +", toString(names(test.all)[1:16]))
但是,我收到以下错误:
Error in str2lang(x) : :2:0: unexpected end of input
1: cbind(water, evergreen_needleleaf, evergreen_broadleaf, deciduous_needleleaf, deciduous_broadleaf, mixed_forest, closed_shrubland, open_shrubland, woody_savanna, savanna, grassland, wetland, c
前两个回归的示例应类似于以下线性模型代码的结果:
lm0 <- summary(lm(water ~ year + ly_name + pland_00_water, test.all))
lm1 <- summary(lm(evergreen_needleleaf ~ year + ly_name + pland_01_evergreen_needleleaf, test.all))
这是一个可重现的代码:
structure(list(water = c(0.0266106184724444, 0.026137620650439,
0.0238754445359909, 0.0227927493187162, 0.023204407563968, 0.022095910821215,
0.0221039497341211, 0.0224025055623282, 0.0232403555562322, 0.0273998728633078
), evergreen_needleleaf = c(0.0137471744871003, 0.0144035887247754,
0.0138856486918342, 0.0145019940873407, 0.0149325097828957, 0.0150618713973477,
0.0154319309642015, 0.0161810020776188, 0.0170900682794849, 0.0202759811196201
), evergreen_broadleaf = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), deciduous_needleleaf = c(0.0221575690786241,
0.0219802635560976, 0.0199185261777039, 0.0199446991801378, 0.0197045143048577,
0.0183598706245923, 0.0179199139516306, 0.0185305832647621, 0.0193142162045703,
0.0228628117226465), deciduous_broadleaf = c(0.0238458345995142,
0.0232040857587486, 0.0214841381241148, 0.0209491279899662, 0.0212548157790288,
0.0202963468270238, 0.0207337574553642, 0.0215100457093465, 0.022555778652742,
0.0263876257689971), mixed_forest = c(0.0203303324332739, 0.0201326188512546,
0.018766543404341, 0.0187774463529825, 0.0186676568232967, 0.0174556649459849,
0.0176766072202398, 0.0181129640698455, 0.0188309921983036, 0.0223457070048949
), closed_shrubland = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), open_shrubland = c(0,
0, 0, 0, 0.0183556317428838, 0.0164060513198784, 0.0183293519257576,
0, 0, 0), woody_savanna = c(0.0233285060977034, 0.0228552189157265,
0.0212910937007495, 0.020736643493845, 0.0211172942756978, 0.0199027704665565,
0.0202870449080278, 0.0209920283985866, 0.0218169780896148, 0.0255779305256582
), savanna = c(0.0248772696744645, 0.0242015300056006, 0.0229098819059569,
0.0216992148267621, 0.02232457051906, 0.0212990484551898, 0.0217802849914084,
0.0220095387934639, 0.022631143642652, 0.0263675007279951), grassland = c(0.0238904482108103,
0.0235816188910161, 0.0217520487475381, 0.0212130792799907, 0.0216215195981497,
0.0204580319290287, 0.0206732092067514, 0.0213949117746571, 0.0224477080283398,
0.0262118230104858), wetland = c(0.0264128405590059, 0.0259547241874682,
0.0236664903823689, 0.0225855806415053, 0.0230617689890772, 0.0218049812953526,
0.0217860302384294, 0.0222739293864217, 0.0232776881931578, 0.0274338405264984
), cropland = c(0.0248336521952326, 0.0244080890876927, 0.0225526215748339,
0.0217545122014622, 0.0222376235302143, 0.021005482435398, 0.0212045945848368,
0.0218367797830206, 0.0227718463775454, 0.0265531833360918),
urban = c(0.0250646664786252, 0.0246561168749397, 0.0228185180408942,
0.0220603734374631, 0.0224816863019172, 0.0213385988560285,
0.0217711511602731, 0.0223094451530462, 0.0234921256975096,
0.0272808182572299), mosiac = c(0.0249565082975979, 0.0241971741365792,
0.0228440940044708, 0.0218895038420071, 0.0222927526503101,
0.0213319832723955, 0.0221657387873506, 0.0224140379666739,
0.0235021369267494, 0.0274166095678471), barren = c(0.0253888516856524,
0.024922286505002, 0.0228034238016079, 0.0225570441579643,
0.0231818346072287, 0.0222479996483801, 0.0222857522288144,
0.0229085001302335, 0.0244504815466996, 0.0282947587930509
), year = c(2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
2018, 2019), ly_name = c("Prairie_Potholes", "Prairie_Potholes",
"Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes",
"Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes",
"Prairie_Potholes", "Prairie_Potholes"), 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_03_deciduous_needleleaf = c(0.0933699625942604,
0.120129601358389, 0.159210224877223, 0.180001886650791,
0.205606433094165, 0.21696484091491, 0.21696484091491, 0.215617233207364,
0.191167779084743, 0.154782370981001), pland_04_deciduous_broadleaf = c(1.11177635872547,
1.1460440975745, 1.09310236620662, 1.10041795090473, 1.07789365065003,
1.02167915770668, 1.03072738088592, 0.884030656150193, 0.693440423225826,
0.568112906424046), pland_05_mixed_forest = c(0.124172424481027,
0.203103733065865, 0.209649256216803, 0.217157356301702,
0.202718702292281, 0.177114155848906, 0.156515009462131,
0.108193647377267, 0.132258070726303, 0.149006909377232),
pland_07_open_shrubland = c(0, 0, 0, 0, 0.000385030773584579,
0.000385030773584579, 0.000192515386792289, 0, 0, 0), pland_08_woody_savanna = c(1.98310099934737,
2.04066309999827, 2.08455660818691, 2.26552107177166, 2.26937137950751,
1.97905817622473, 1.78211493553622, 1.78943052023433, 2.19910326332832,
1.93631976035685), pland_09_savanna = c(2.19794817100757,
2.38372551926213, 2.48999401277147, 2.56623010594122, 2.87290711710133,
3.16572302041241, 3.09141208111058, 3.13068522001621, 2.91603056374281,
2.52214408236578), pland_10_grassland = c(33.5914322952262,
33.4945970556697, 34.4152056353104, 34.7370913620271, 35.7002458421489,
38.465921888807, 35.9177882292242, 40.7439564607201, 39.9819805597962,
36.9540985563271), pland_11_wetland = c(1.66121527263067,
1.8525755671022, 1.87702502122482, 1.99022406865869, 2.16868583221514,
2.38565067313005, 2.59606999089402, 2.39354380398853, 2.30479421067729,
2.22759554057358), pland_12_cropland = c(56.1875407891976,
55.5068063815, 54.4165917460953, 53.4270626579829, 51.6459102993807,
48.541599687355, 50.9228225065888, 46.0841407749514, 47.2484738342712,
51.2293070023622), pland_13_urban = c(0.288388049414849,
0.303789280358233, 0.320538119009162, 0.339982173075183,
0.362698988716673, 0.386378381292125, 0.404859858424185,
0.426806612518506, 0.441630297301512, 0.451448582027919),
pland_14_mosiac = c(1.58613427178167, 1.63888348776276, 1.55841205608358,
1.73879897350796, 1.98040578393228, 2.10592581612085, 2.34811017270555,
2.66787823016755, 2.381992880781, 2.42357620432813), pland_15_barren = c(0.39273138905627,
0.374057396537418, 0.395041573697778, 0.404282312263808,
0.421993727848698, 0.44143778191472, 0.438742566499627, 0.487641474744869,
0.478400736178839, 0.433352135669443), pland_06_closed_shrubland = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), pland_02_evergreen_broadleaf = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame")
你还需要把公式放在一起:
Vars = c("pland_00_water", "pland_01_evergreen_needleleaf", "pland_02_evergreen_broadleaf","pland_03_deciduous_needleleaf", "pland_04_deciduous_broadleaf" , "pland_05_mixed_forest",
"pland_06_closed_shrubland","pland_07_open_shrubland",
"pland_08_woody_savanna","pland_09_savanna" ,
"pland_10_grassland","pland_11_wetland" ,
"pland_12_cropland" ,"pland_13_urban" ,
"pland_14_mosiac" ,"pland_15_barren")
t1 <- lapply(Vars,
function(x) summary(lm(as.formula(paste(string_formula,x)), test.all))
)
t1[[1]][[1]]]
Call:
lm(formula = water ~ year + pland_00_water, data = test.all)
Residuals:
Min 1Q Median 3Q Max
-0.0010544 -0.0006921 -0.0002486 0.0007537 0.0014454
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.2638399 0.2677866 -0.985 0.35732
year 0.0001521 0.0001338 1.137 0.29309
pland_00_water -0.0199263 0.0042267 -4.714 0.00217 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.001074 on 7 degrees of freedom
Multiple R-squared: 0.772, Adjusted R-squared: 0.7069
F-statistic: 11.85 on 2 and 7 DF, p-value: 0.005658
或者像这样:
ma = as.matrix(test.all[,1:16])
models <- lapply(Vars,function(x){
mdl = reformulate(response="mat",termlabels = c("year",x))
summary(lm(mdl,data=test.all))
})
Call:
lm(formula = water ~ year + pland_00_water, data = test.all)
Residuals:
Min 1Q Median 3Q Max
-0.0010544 -0.0006921 -0.0002486 0.0007537 0.0014454
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.2638399 0.2677866 -0.985 0.35732
year 0.0001521 0.0001338 1.137 0.29309
pland_00_water -0.0199263 0.0042267 -4.714 0.00217 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.001074 on 7 degrees of freedom
Multiple R-squared: 0.772, Adjusted R-squared: 0.7069
F-statistic: 11.85 on 2 and 7 DF, p-value: 0.005658
我正在尝试为线性回归模型遍历多个 Y 和 X 变量。
想法是在每个单独的 X 变量上循环 Y1 变量,然后在每个 X 变量上循环 Y2 等等...
类似于:Y1 ... Yn ~ X .. Xn
代码看起来像这样:
t1 <- lapply(
c("pland_00_water", "pland_01_evergreen_needleleaf", "pland_02_evergreen_broadleaf","pland_03_deciduous_needleleaf", "pland_04_deciduous_broadleaf" , "pland_05_mixed_forest",
"pland_06_closed_shrubland" , "pland_07_open_shrubland" , "pland_08_woody_savanna" , "pland_09_savanna" ,
"pland_10_grassland" , "pland_11_wetland" , "pland_12_cropland" , "pland_13_urban" ,
"pland_14_mosiac" , "pland_15_barren"),
function(x) summary(lm(as.formula(string_formula, x), test.all))
)
其中字符串公式是这个代码:
string_formula <- sprintf("cbind(%s) ~ year +", toString(names(test.all)[1:16]))
但是,我收到以下错误:
Error in str2lang(x) : :2:0: unexpected end of input 1: cbind(water, evergreen_needleleaf, evergreen_broadleaf, deciduous_needleleaf, deciduous_broadleaf, mixed_forest, closed_shrubland, open_shrubland, woody_savanna, savanna, grassland, wetland, c
前两个回归的示例应类似于以下线性模型代码的结果:
lm0 <- summary(lm(water ~ year + ly_name + pland_00_water, test.all))
lm1 <- summary(lm(evergreen_needleleaf ~ year + ly_name + pland_01_evergreen_needleleaf, test.all))
这是一个可重现的代码:
structure(list(water = c(0.0266106184724444, 0.026137620650439,
0.0238754445359909, 0.0227927493187162, 0.023204407563968, 0.022095910821215,
0.0221039497341211, 0.0224025055623282, 0.0232403555562322, 0.0273998728633078
), evergreen_needleleaf = c(0.0137471744871003, 0.0144035887247754,
0.0138856486918342, 0.0145019940873407, 0.0149325097828957, 0.0150618713973477,
0.0154319309642015, 0.0161810020776188, 0.0170900682794849, 0.0202759811196201
), evergreen_broadleaf = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), deciduous_needleleaf = c(0.0221575690786241,
0.0219802635560976, 0.0199185261777039, 0.0199446991801378, 0.0197045143048577,
0.0183598706245923, 0.0179199139516306, 0.0185305832647621, 0.0193142162045703,
0.0228628117226465), deciduous_broadleaf = c(0.0238458345995142,
0.0232040857587486, 0.0214841381241148, 0.0209491279899662, 0.0212548157790288,
0.0202963468270238, 0.0207337574553642, 0.0215100457093465, 0.022555778652742,
0.0263876257689971), mixed_forest = c(0.0203303324332739, 0.0201326188512546,
0.018766543404341, 0.0187774463529825, 0.0186676568232967, 0.0174556649459849,
0.0176766072202398, 0.0181129640698455, 0.0188309921983036, 0.0223457070048949
), closed_shrubland = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), open_shrubland = c(0,
0, 0, 0, 0.0183556317428838, 0.0164060513198784, 0.0183293519257576,
0, 0, 0), woody_savanna = c(0.0233285060977034, 0.0228552189157265,
0.0212910937007495, 0.020736643493845, 0.0211172942756978, 0.0199027704665565,
0.0202870449080278, 0.0209920283985866, 0.0218169780896148, 0.0255779305256582
), savanna = c(0.0248772696744645, 0.0242015300056006, 0.0229098819059569,
0.0216992148267621, 0.02232457051906, 0.0212990484551898, 0.0217802849914084,
0.0220095387934639, 0.022631143642652, 0.0263675007279951), grassland = c(0.0238904482108103,
0.0235816188910161, 0.0217520487475381, 0.0212130792799907, 0.0216215195981497,
0.0204580319290287, 0.0206732092067514, 0.0213949117746571, 0.0224477080283398,
0.0262118230104858), wetland = c(0.0264128405590059, 0.0259547241874682,
0.0236664903823689, 0.0225855806415053, 0.0230617689890772, 0.0218049812953526,
0.0217860302384294, 0.0222739293864217, 0.0232776881931578, 0.0274338405264984
), cropland = c(0.0248336521952326, 0.0244080890876927, 0.0225526215748339,
0.0217545122014622, 0.0222376235302143, 0.021005482435398, 0.0212045945848368,
0.0218367797830206, 0.0227718463775454, 0.0265531833360918),
urban = c(0.0250646664786252, 0.0246561168749397, 0.0228185180408942,
0.0220603734374631, 0.0224816863019172, 0.0213385988560285,
0.0217711511602731, 0.0223094451530462, 0.0234921256975096,
0.0272808182572299), mosiac = c(0.0249565082975979, 0.0241971741365792,
0.0228440940044708, 0.0218895038420071, 0.0222927526503101,
0.0213319832723955, 0.0221657387873506, 0.0224140379666739,
0.0235021369267494, 0.0274166095678471), barren = c(0.0253888516856524,
0.024922286505002, 0.0228034238016079, 0.0225570441579643,
0.0231818346072287, 0.0222479996483801, 0.0222857522288144,
0.0229085001302335, 0.0244504815466996, 0.0282947587930509
), year = c(2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
2018, 2019), ly_name = c("Prairie_Potholes", "Prairie_Potholes",
"Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes",
"Prairie_Potholes", "Prairie_Potholes", "Prairie_Potholes",
"Prairie_Potholes", "Prairie_Potholes"), 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_03_deciduous_needleleaf = c(0.0933699625942604,
0.120129601358389, 0.159210224877223, 0.180001886650791,
0.205606433094165, 0.21696484091491, 0.21696484091491, 0.215617233207364,
0.191167779084743, 0.154782370981001), pland_04_deciduous_broadleaf = c(1.11177635872547,
1.1460440975745, 1.09310236620662, 1.10041795090473, 1.07789365065003,
1.02167915770668, 1.03072738088592, 0.884030656150193, 0.693440423225826,
0.568112906424046), pland_05_mixed_forest = c(0.124172424481027,
0.203103733065865, 0.209649256216803, 0.217157356301702,
0.202718702292281, 0.177114155848906, 0.156515009462131,
0.108193647377267, 0.132258070726303, 0.149006909377232),
pland_07_open_shrubland = c(0, 0, 0, 0, 0.000385030773584579,
0.000385030773584579, 0.000192515386792289, 0, 0, 0), pland_08_woody_savanna = c(1.98310099934737,
2.04066309999827, 2.08455660818691, 2.26552107177166, 2.26937137950751,
1.97905817622473, 1.78211493553622, 1.78943052023433, 2.19910326332832,
1.93631976035685), pland_09_savanna = c(2.19794817100757,
2.38372551926213, 2.48999401277147, 2.56623010594122, 2.87290711710133,
3.16572302041241, 3.09141208111058, 3.13068522001621, 2.91603056374281,
2.52214408236578), pland_10_grassland = c(33.5914322952262,
33.4945970556697, 34.4152056353104, 34.7370913620271, 35.7002458421489,
38.465921888807, 35.9177882292242, 40.7439564607201, 39.9819805597962,
36.9540985563271), pland_11_wetland = c(1.66121527263067,
1.8525755671022, 1.87702502122482, 1.99022406865869, 2.16868583221514,
2.38565067313005, 2.59606999089402, 2.39354380398853, 2.30479421067729,
2.22759554057358), pland_12_cropland = c(56.1875407891976,
55.5068063815, 54.4165917460953, 53.4270626579829, 51.6459102993807,
48.541599687355, 50.9228225065888, 46.0841407749514, 47.2484738342712,
51.2293070023622), pland_13_urban = c(0.288388049414849,
0.303789280358233, 0.320538119009162, 0.339982173075183,
0.362698988716673, 0.386378381292125, 0.404859858424185,
0.426806612518506, 0.441630297301512, 0.451448582027919),
pland_14_mosiac = c(1.58613427178167, 1.63888348776276, 1.55841205608358,
1.73879897350796, 1.98040578393228, 2.10592581612085, 2.34811017270555,
2.66787823016755, 2.381992880781, 2.42357620432813), pland_15_barren = c(0.39273138905627,
0.374057396537418, 0.395041573697778, 0.404282312263808,
0.421993727848698, 0.44143778191472, 0.438742566499627, 0.487641474744869,
0.478400736178839, 0.433352135669443), pland_06_closed_shrubland = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), pland_02_evergreen_broadleaf = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame")
你还需要把公式放在一起:
Vars = c("pland_00_water", "pland_01_evergreen_needleleaf", "pland_02_evergreen_broadleaf","pland_03_deciduous_needleleaf", "pland_04_deciduous_broadleaf" , "pland_05_mixed_forest",
"pland_06_closed_shrubland","pland_07_open_shrubland",
"pland_08_woody_savanna","pland_09_savanna" ,
"pland_10_grassland","pland_11_wetland" ,
"pland_12_cropland" ,"pland_13_urban" ,
"pland_14_mosiac" ,"pland_15_barren")
t1 <- lapply(Vars,
function(x) summary(lm(as.formula(paste(string_formula,x)), test.all))
)
t1[[1]][[1]]]
Call:
lm(formula = water ~ year + pland_00_water, data = test.all)
Residuals:
Min 1Q Median 3Q Max
-0.0010544 -0.0006921 -0.0002486 0.0007537 0.0014454
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.2638399 0.2677866 -0.985 0.35732
year 0.0001521 0.0001338 1.137 0.29309
pland_00_water -0.0199263 0.0042267 -4.714 0.00217 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.001074 on 7 degrees of freedom
Multiple R-squared: 0.772, Adjusted R-squared: 0.7069
F-statistic: 11.85 on 2 and 7 DF, p-value: 0.005658
或者像这样:
ma = as.matrix(test.all[,1:16])
models <- lapply(Vars,function(x){
mdl = reformulate(response="mat",termlabels = c("year",x))
summary(lm(mdl,data=test.all))
})
Call:
lm(formula = water ~ year + pland_00_water, data = test.all)
Residuals:
Min 1Q Median 3Q Max
-0.0010544 -0.0006921 -0.0002486 0.0007537 0.0014454
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.2638399 0.2677866 -0.985 0.35732
year 0.0001521 0.0001338 1.137 0.29309
pland_00_water -0.0199263 0.0042267 -4.714 0.00217 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.001074 on 7 degrees of freedom
Multiple R-squared: 0.772, Adjusted R-squared: 0.7069
F-statistic: 11.85 on 2 and 7 DF, p-value: 0.005658