Quantcast
Channel: Active questions tagged lm - Stack Overflow
Viewing all articles
Browse latest Browse all 124

loop regression when column names have special characters

$
0
0

Dummy data shown below. I tried amending my loop regression code to account for spaces in the column names, but it still gives an error. Additionally, I'm not sure how to adjust .x to account for spaces in the independent variable column names too.The purpose of the code is to regress the first column against each of the subsequent columns one at a time. i.e. Canada Price ~ Global Price, then Canada Price ~ Canada Bond Price and so on.

lm.test <- map_dfr(  set_names(names(df)),  ~ tidy(lm(as.formula(paste("`",colnames(df[1]),"`","~", .x)),            data = df)),  .id = "predictor") 
Canada PriceGlobal PriceCanada 2Y Bond PriceUS - Canada InflationOil Price
-0.030661468-0.0002079950.084-0.0750.002116
-0.034269304-0.1402121410.363-1.062-0.06122
0.023714640.019987-0.7810.2730.209895
-0.0079714840.090597341-0.2210.068-0.10657
0.0053427160.0606271490.1510.234-0.22191
-0.0022103760.046010712-0.1060.130.073975
0.0183382990.0197995340.0360.0050.058091
0.000662471-0.0253560470.5230.045-0.03765
0.0021244110.045979342-0.4330.2820.365933
0.0341915550.043211347-0.080.1470.113365
-0.011470069-0.0337731760.396-0.037-0.09753
0.0070398470.050327089-0.0520.1510.145487
-0.028872959-0.0045013710.324-0.2140.064282
0.009244373-0.000924130.166-0.0660.076961
0.017448201-0.010638258-0.1790.0530.108548
-0.0023798190.0501116740.352-0.161-0.11261
-0.0021034060.030948930.1210.1060.130575
0.0191549690.083073750.0510.1490.041074
-0.00124318-0.0545269540.3450.0320.079687
-0.0012416360.003319001-0.2890.2190.100941

Edit

Data in dput format.

df1 <-  structure(list(    `Canada Price` = c(-0.030661468, -0.034269304, 0.02371464, -0.007971484,                        0.005342716, -0.002210376, 0.018338299, 0.000662471, 0.002124411,                        0.034191555, -0.011470069, 0.007039847, -0.028872959, 0.009244373,                        0.017448201, -0.002379819, -0.002103406, 0.019154969, -0.00124318, -0.001241636),     `Global Price` = c(-0.000207995, -0.140212141, 0.019987, 0.090597341,                        0.060627149, 0.046010712, 0.019799534, -0.025356047, 0.045979342,                       0.043211347, -0.033773176, 0.050327089, -0.004501371, -0.00092413,                        -0.010638258, 0.050111674, 0.03094893, 0.08307375, -0.054526954, 0.003319001),     `Canada 2Y Bond Price` = c(0.084, 0.363, -0.781, -0.221, 0.151, -0.106,                                0.036, 0.523, -0.433, -0.08, 0.396, -0.052, 0.324, 0.166, -0.179, 0.352,                               0.121, 0.051, 0.345, -0.289),     `US - Canada Inflation` = c(-0.075, -1.062, 0.273, 0.068,                                 0.234, 0.13, 0.005, 0.045, 0.282, 0.147, -0.037, 0.151, -0.214,                                 -0.066, 0.053, -0.161, 0.106, 0.149, 0.032, 0.219),     `Oil Price` = c(0.002116, -0.06122, 0.209895, -0.10657, -0.22191, 0.073975,                     0.058091, -0.03765, 0.365933, 0.113365, -0.09753, 0.145487, 0.064282,                     0.076961, 0.108548, -0.11261, 0.130575, 0.041074, 0.079687, 0.100941)),     class = "data.frame", row.names = c(NA, -20L))

Viewing all articles
Browse latest Browse all 124

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>