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 Price | Global Price | Canada 2Y Bond Price | US - Canada Inflation | Oil Price |
|---|---|---|---|---|
| -0.030661468 | -0.000207995 | 0.084 | -0.075 | 0.002116 |
| -0.034269304 | -0.140212141 | 0.363 | -1.062 | -0.06122 |
| 0.02371464 | 0.019987 | -0.781 | 0.273 | 0.209895 |
| -0.007971484 | 0.090597341 | -0.221 | 0.068 | -0.10657 |
| 0.005342716 | 0.060627149 | 0.151 | 0.234 | -0.22191 |
| -0.002210376 | 0.046010712 | -0.106 | 0.13 | 0.073975 |
| 0.018338299 | 0.019799534 | 0.036 | 0.005 | 0.058091 |
| 0.000662471 | -0.025356047 | 0.523 | 0.045 | -0.03765 |
| 0.002124411 | 0.045979342 | -0.433 | 0.282 | 0.365933 |
| 0.034191555 | 0.043211347 | -0.08 | 0.147 | 0.113365 |
| -0.011470069 | -0.033773176 | 0.396 | -0.037 | -0.09753 |
| 0.007039847 | 0.050327089 | -0.052 | 0.151 | 0.145487 |
| -0.028872959 | -0.004501371 | 0.324 | -0.214 | 0.064282 |
| 0.009244373 | -0.00092413 | 0.166 | -0.066 | 0.076961 |
| 0.017448201 | -0.010638258 | -0.179 | 0.053 | 0.108548 |
| -0.002379819 | 0.050111674 | 0.352 | -0.161 | -0.11261 |
| -0.002103406 | 0.03094893 | 0.121 | 0.106 | 0.130575 |
| 0.019154969 | 0.08307375 | 0.051 | 0.149 | 0.041074 |
| -0.00124318 | -0.054526954 | 0.345 | 0.032 | 0.079687 |
| -0.001241636 | 0.003319001 | -0.289 | 0.219 | 0.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))
Here is a solution to run regressions on all other columns but the first.
The formula is created with
reformulate.Created on 2024-02-23 with reprex v2.0.2
Edit
After running
summaryon the output list, you can extract the values you want from this second list (of summaries).Created on 2024-02-23 with reprex v2.0.2