简体   繁体   English

R回归循环

[英]R Regression Loop

I am attempting to write an R script that will run through a regression for every 1000 rows of data. 我正在尝试编写一个R脚本,该脚本将为每1000行数据运行一次回归。

I would also like to store the output into a csv file. 我还想将输出存储到csv文件中。

Data Link: https://drive.google.com/open?id=0B-W2Dyw4QjoMQ0F1ZFhsdWJORHM 数据链接: https//drive.google.com/open?id = 0B-W2Dyw4QjoMQ0F1ZFhsdWJORHM

Any help would be great! 任何帮助都会很棒!

 z <- read.csv("Merge.csv", sep=",", header=TRUE, stringsAsFactors=FALSE)


co<-matrix(NA, nrow=44, ncol = 7)

## Regression for mkt returns and 7 variables 
for (i in 5:44){
  cat(i)
  temp <- lm(ret~earningsyld+book2mkt+leverage+corptax+financials+Momentum+lnmktcap, na.action="na.omit", data = z[1000*(i-1)+1:1000*i,])
  co[i,]<- summary(temp)$coefficient [2:8,1]
}
# Built-in example data
data("mtcars")
mtcars <- rbind(mtcars,mtcars,mtcars,mtcars,mtcars,mtcars,
                mtcars,mtcars,mtcars,mtcars,mtcars)

# Let's pretend that instead of 1000 rows it's 10, so that I can use builtin data to show you

co<-matrix(NA, nrow=35, ncol = 1)

## Regressions 
for (i in 1:floor(nrow(mtcars)/10)){
  cat(i)
  temp   <- lm(mpg~cyl, na.action="na.omit", data = mtcars[((i-1)*10)+1:(i*10),])
  co[i,] <- summary(temp)$coefficients[2:2,1]
}

head(co)
  [,1] [1,] -1.696939 [2,] -3.461047 [3,] -2.572180 [4,] -2.651129 [5,] -3.023638 [6,] -2.712391 

I believe there was a problem with your indexing as well as an incorrect spelling of the coefficients element of the summary object. 我相信您的索引存在问题以及摘要对象的coefficients元素的拼写错误。 Any other issues would be data issues, so if you still have problems after this be sure to provide the data. 任何其他问题都是数据问题,所以如果在此之后仍然有问题,请务必提供数据。

For example if a problem in the data (singularity, all missing values, no identifying variation, etc) causes the inestimitability of a coefficient then it will change the size of the returned summary and break your loop. 例如,如果数据中的问题(奇点,所有缺失值,没有识别变化等)导致系数的不允许性,那么它将改变返回的摘要的大小并打破循环。 The only solution to that would be to clean your data or change your model to avoid using bad data. 唯一的解决方案是清理数据或更改模型以避免使用错误数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM