I am calculating residuals from many regressions to plot these residuals afterwards with ggplot.
I took the 6 variables of interest from my datafile and created the subfieldsdata file (6 columns, 116 rows).
The linear regression models:
buildres <- sapply(seq_along(subfieldsdata), function(i) lm(subfieldsdata[,i] ~ myData$NP_Age+myData$sex+myData$YrsOfEd), simplify=FALSE, USE.NAMES=TRUE)
Calculation of the residuals:
getres <- vapply(buildres, stdres, numeric(116))
Adding column names:
colnames(getres) <- c("Subiculum residuals", "Presubiculum residuals", "CA1 residuals", "CA3 residuals", "CA4 residuals", "Dentate gyrus residuals")
Then I have a for loop for ggplot:
for (i in 1:ncol (getres)) {
gp <- ggplot(data=subfieldsdata,aes(x=ECtau.res,y=getres[,i])) + geom_point(colour="Blue", shape=17, size=5)
gp <- gp +
stat_smooth(method="lm", colour="Blue", size=2, fill="Blue") +
scale_y_continuous(breaks=seq(-4, max(getres[,i])*1.1, 0.5)) +
theme_grey(base_size=35) +
labs(x="EC tau residuals", y=colnames(getres)[i]))
print (gp)
ggsave(sprintf("ECtau%s.png", colnames((getres)[i]),gp))
}
I get this error message:
Saving 7 x 7 in image
Error in strsplit(filename, "\\.")[[1]] : subscript out of bounds
I cannot be the dimensions:
dim(getres)
[1] 116 6
I also checked the colnames:
> colnames(getres)
[1] "Subiculum residuals" "Presubiculum residuals" "CA1 residuals" "CA3 residuals"
[5] "CA4 residuals" "Dentate gyrus residuals"
Do you know what might be the problem?
Thanks!