Warning meassage: number of items to replace is not a multiple of replacement length

StackOverflow https://stackoverflow.com/questions/23649281

  •  22-07-2023
  •  | 
  •  

Question

I got warnings when running this code.

For example, when I put tm1<- summary(tmfit)[c(4,8,9)], I can get the result, but I need to run this code for each $i$.

Why do I get this error?

Is there any way to do this instead of via a for loop?

Specifically, I have many regressants ($y$) with the same two regressors ($x$'s).

How I can get these results of regression analysis(to make some comparisons)?

dreg=read.csv("dayreg.csv")
fundr=read.csv("fundreturnday.csv")
num=ncol(fundr)
exr=dreg[,2]
tm=dreg[,4]
for(i in 2:num)
{
  tmfit=lm(fundr[,i]~exr+tm)
  tm1[i]<- summary(tmfit)[c(4,8,9)]
}

Any help is highly appreciated

Was it helpful?

Solution

Try storing your result into a list instead of a vector.

dreg=read.csv("dayreg.csv")
fundr=read.csv("fundreturnday.csv")
num=ncol(fundr)
exr=dreg[,2]
tm = list()
for(i in 2:num)
{
  tmfit=lm(fundr[,i]~exr+tm)
  tm1[[i]]<- summary(tmfit)[c(4,8,9)]
}

You can look at an element in the list like so

tm1[[2]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top