Reputation: 1855
I want to automatically run linear regressions and save the results. The beta coefficients generated by R project will be later used as parameters for further computations.
This an example of my data layout:
id |x_2000|x_2001|x_2002|y_2000|y_2001|y_2002|z_2000|z_2001|z_2002
1 |20 |NA |6 |90 |NA |80 |54 |NA |10
2 |50 |NA |10 |50 |NA |50 |60 |NA |40
3 |4 |NA |1 |5 |NA |10 |30 |NA |120
x is value x and the number behind it represents a year. The same logic applies to the other variables y and z.
To run the linear regressions, I have created a loop. I use the following code to loop through the variables and to run a regression for each year.
for (i in 2000:2002){
X_COLUMN <- c(paste0("x_",i))
Y_COLUMN <- c(paste0("y_",i))
Z_COLUMN <- c(paste0("z_",i))
result.lm <- lm(as.formula(paste("formula=",X_COLUMN,"~",Y_COLUMN,"+",Z_COLUMN,"-1")), data=data_for_regression)
b1 <- rbind(b1, c(x,i,coef(result.lm)[1]))
b2 <- rbind(b2, c(x,i,coef(result.lm)[2]))
}
For 2000 everything works well, however when the loop continues to year 2001 it hits the NA values. This results in a error message:
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : 0 (non-NA) cases
Logical, because there is no complete case for year 2001. The result is that the loop breaks. However, I don't want it to break but to continue to the next year.
Any idea how to fix this?
Upvotes: 0
Views: 512
Reputation: 206232
How about using a try
statement
for (i in 2000:2002){
X_COLUMN <- c(paste0("x_",i))
Y_COLUMN <- c(paste0("y_",i))
Z_COLUMN <- c(paste0("z_",i))
try({
result.lm <- lm(as.formula(paste("formula=",X_COLUMN,"~",Y_COLUMN,"+",Z_COLUMN,"-1")), data=data_for_regression)
b1 <- rbind(b1, c(x,i,coef(result.lm)[1]))
b2 <- rbind(b2, c(x,i,coef(result.lm)[2]))
}, silent=T)
}
or an tryCatch
for (i in 2000:2002){
X_COLUMN <- c(paste0("x_",i))
Y_COLUMN <- c(paste0("y_",i))
Z_COLUMN <- c(paste0("z_",i))
tryCatch({
result.lm <- lm(as.formula(paste("formula=",X_COLUMN,"~",Y_COLUMN,"+",Z_COLUMN,"-1")), data=data_for_regression)
b1 <- rbind(b1, c(x,i,coef(result.lm)[1]))
b2 <- rbind(b2, c(x,i,coef(result.lm)[2]))
}, error=function(e) {
b1 <- rbind(b1, c(x,i,NA))
b2 <- rbind(b2, c(x,i,NA))
})
}
Upvotes: 0