I am trying to loop my multiple linear regression plot and summaries, but I keep encountering an error in R that states Error: More than one expression parsed
. I am not sure how to fix this or if there is a better way to achieve what I want to do which is mainly:
- Plot a multiple linear regression plot with
Group
as the colour - Get summary for each of the linear regression lines based on
Group
- Compute regression summary
- Perform anova to determine differences
colNames <- names(df)[c(35:39)]
for(i in colNames){
plt <- ggplot(df,
aes_string(x=df$MachineLength, y=i, fill=df$Group, color=be_nlyl$Group)) +
geom_smooth(method=lm) +
geom_point(size = 2, alpha=0.7) +
labs(title="Machine", subtitle = "Machine Type") +
theme_bw() +
theme(plot.title = element_text(hjust=0.5, face="bold"),
plot.subtitle = element_text(hjust=0.5))
print(plt)
lm_A <- lm(formula = i ~ MachineLength, data = subset(be_nlyl, Group == "A"))
summary(lm_A) %>% print()
lm_B <- lm(formula = i ~ MachineLength, data = subset(be_nlyl, Group == "B"))
summary(lm_B) %>% print()
clz.lm <- lm(formula = i ~ Group + MachineLength + Group:MachineLength, data = df)
summary(clz.lm) %>% print()
ano.lm <- Anova(lm(i ~ MachineLength*Group, data = df))
print(ano.lm)
}
Anyone have ideas of how to implement above? Thank you!