Home > Back-end >  Loop for lm-function in R? Seperate output data
Loop for lm-function in R? Seperate output data

Time:10-28

I have the following code:

set.seed(12345)
a <- rnorm(852) 
b <- rnorm(852)
abc <- lm(a ~ b)
summary(abc)

Now I want to determine different coefficients using lm-function with the following values:

lm1 <- lm(a[1:52] ~ b[1:52])
lm2 <- lm(a[2:53] ~ b[2:53])
lm3 <- lm(a[3:54] ~ b[3:54])
.....
lm801 <- lm(a[801:852] ~ b[801:852])

I looking for a reproducible solution so I don't have to enter all values individually. A vector with all 801 coefficients would be optimal as a solution.

If anyone knows what this type of "partial regression" is called in mathematics, they are welcome to share the technical term. Many Thanks.

CodePudding user response:

Package rollRegres solution

If you only want the coefficients there is contributed package rollRegres. In the code below, I create a data.frame with vectors a and b.

library(rollRegres)
df1 <- data.frame(a, b)
lm1 <- roll_regres(a ~ b, df1, width = 52, do_compute = c("sigmas", "r.squareds"))
str(lm1)

Package roll solution

Similar, with package roll.

library(roll)

lm2 <- roll_lm(b, a, width = 52L)
str(lm2)

Base R

set.seed(12345)
a <- rnorm(852) 
b <- rnorm(852)
abc <- lm(a ~ b)
#summary(abc)

n <- length(a)
size <- 52L
starts <- seq.int(n - size   1L)
ends <- seq.int(n)[-(seq.int(size) - 1L)]

df1 <- data.frame(a, b)

lm_list <- mapply(\(i, j, X) lm(a ~ b, data = X[i:j, ]), starts, ends, 
                  MoreArgs = list(X = df1), SIMPLIFY = FALSE)
summary(lm_list[[1]])
#> 
#> Call:
#> lm(formula = a ~ b, data = X[i:j, ])
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -2.27602 -0.79302  0.04888  0.68129  1.99165 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)  
#> (Intercept)   0.2477     0.1498   1.653   0.1045  
#> b             0.3427     0.1567   2.187   0.0334 *
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.068 on 50 degrees of freedom
#> Multiple R-squared:  0.08734,    Adjusted R-squared:  0.06909 
#> F-statistic: 4.785 on 1 and 50 DF,  p-value: 0.03341

Created on 2022-10-28 with reprex v2.0.2

CodePudding user response:

If you only need the coefficients, lm.fit should be sufficient.

res <- t(sapply(0:800, \(i) lm.fit(cbind(1, b[1:52   i]), a[1:52   i])$coef))

head(res)
#             x1        x2
# [1,] 0.2476502 0.3426848
# [2,] 0.2353523 0.3471548
# [3,] 0.2297629 0.3468818
# [4,] 0.2344840 0.3841514
# [5,] 0.2670906 0.3790527
# [6,] 0.2666326 0.3871418

dim(res)
# [1] 801   2
  • Related