I have a generic csv file with 8 columns (y,x1,...,x7), with y representing a response variable and the x's representing potential predictors, each with 100 observations.
My goals are to use R to...
1 - create 7 scatterplots of y vs each one of the x's.
2 - create a linear model of y with each of the x's.
Currently I just have it all typed out repetitively...
p9 <- ggplot(generic, aes(x1,y)) + geom_point() + labs(title = "y vs x1")
p10 <- ggplot(generic, aes(x2,y)) + geom_point() + labs(title = "y vs x2")
p11 <- ggplot(generic, aes(x3,y)) + geom_point() + labs(title = "y vs x3")
p12 <- ggplot(generic, aes(x4,y)) + geom_point() + labs(title = "y vs x4")
p13 <- ggplot(generic, aes(x5,y)) + geom_point() + labs(title = "y vs x5")
p14 <- ggplot(generic, aes(x6,y)) + geom_point() + labs(title = "y vs x6")
p15 <- ggplot(generic, aes(x7,y)) + geom_point() + labs(title = "y vs x7")
grid.arrange(p9,p10,p11,p12,p13,p14,p15,ncol = 3)
and
x1mod <- lm(generic$y~generic$x1)
x2mod <- lm(generic$y~generic$x2)
x3mod <- lm(generic$y~generic$x3)
x4mod <- lm(generic$y~generic$x4)
x5mod <- lm(generic$y~generic$x5)
x6mod <- lm(generic$y~generic$x6)
x7mod <- lm(generic$y~generic$x7)
summary(x1mod)
summary(x2mod)
summary(x3mod)
summary(x4mod)
summary(x5mod)
summary(x6mod)
summary(x7mod)
I would like to make this less repetitive. I attempted to accomplish this with a for loop but it turned into a bit of a mess. I also read up on mapping functions to data with purr but I couldn't quite figure out how to make it work for my situation as I am not trying to divide the data by any factor. I am relatively new to R so I apologize if my issue is laughably simple.