1
votes

I have two xts datasets. Each containing 50 variables. One is dependent variables dataset other one is independent variables dataset. I want to apply multiple regression models, i.e. one variable from each data set. But does not get desired results. Many values are missing. I want to extract coefficients from each regression along with t and p values.

library(xts)
library(zoo)
library(PerformanceAnalytics)
dep_var<-managers[,c(1,3)]
ind_var<-managers[,c(8,9)]
## individually running each model
model1<-lm(dep_var[,1]~ind_var[,1])
model2<-lm(dep_var[,2]~ind_var[,2])
summary(model1)
summary(model2)
models<-lapply(dep_var,function(x) lm(x[i]~ind_var[i]))

1
It doesn't look right. Are you trying to regress using 1 row of dep and ind each time?BellmanEqn
I want to apply regression models model1<- lm(dep_var[,1]~ind_var[,1]. Similiarly model2<- lm(dep_var[,2]~ind_var[,2] and so onsim235
@StupidWolf can you please elloboratesim235
sorry it should be lapply(1:3,function(i) lm(dep_var[,i]~ind_var[,i]))StupidWolf
@sim235 try do.call(rbind,lapply(1:2,function(i) summary(models[[i]])$coefficients))simar

1 Answers

1
votes

Too long to type in comment, what I meant was to write your data.frame in a long format, for example, every row is one observation and the column var indicates like the comparison:

library(broom)
library(dplyr)

dep_var<-data.frame(matrix(runif(30),ncol=3))
colnames(dep_var) = paste0("dv",1:3)
ind_var<-data.frame(matrix(runif(30),ncol=3))
colnames(ind_var) = paste0("iv",1:3)

df = do.call(rbind,lapply(1:3,function(i)data.frame(var=i,indep=ind_var[,i],dep=dep_var[,i])))

head(df)

  var     indep        dep
1   1 0.9138594 0.01566731
2   1 0.8426182 0.09871969
3   1 0.6374313 0.38409883
4   1 0.2891506 0.13790234
5   1 0.2363087 0.60410820
6   1 0.9763011 0.21100213

Then the linear model output can be made nicer by using tidy from broom :

df %>% group_by(var) %>% do(tidy(lm(dep ~ indep,data=.)))
# A tibble: 6 x 6
# Groups:   var [3]
    var term        estimate std.error statistic p.value
  <int> <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1     1 (Intercept)   0.496      0.200     2.48   0.0380
2     1 indep         0.0485     0.382     0.127  0.902 
3     2 (Intercept)   0.459      0.196     2.34   0.0476
4     2 indep        -0.0880     0.356    -0.247  0.811 
5     3 (Intercept)   0.591      0.185     3.19   0.0129
6     3 indep        -0.183      0.270    -0.675  0.519