1

I have a series of statistical models in a list and I would like to plot the coefficients of these. I have various responses and explanatory variables within this list of models, as well as various statistical functions (e.g. exponential and linear).

DF<- as.data.frame(matrix(sample(1:50, 1*2000, replace=TRUE), ncol=4))
colnames(DF) <- c("Response_1","Response_2","Explanatory_1","Explanatory_2")
DF$Factor <- rep(c("Control","Impact"), each = 250)

List_models <- list(lm(Response_1~exp(Explanatory_1):Factor, data=DF), 
                lm(Response_1~Explanatory_2:Factor, data=DF),
                lm(Response_2~Explanatory_1:Factor, data=DF),
                lm(Response_2~exp(Explanatory_2):Factor, data=DF))

I am guessing some kind of lapply function would work here, but I do not know how to do this while changing the function associated with x or change the Response and Explanatory variable being plotted. The code below produces the ideal outcome, but I would like to create this with some kind of loop function.

par(mfrow=c(2,2))
plot(Response_1~Explanatory_1,data=DF,type="n")
curve(List_models[[1]]$coefficients[1]+List_models[[1]]$coefficients[2]*exp(x), add = TRUE)
plot(Response_1~Explanatory_2,data=DF,type="n")
curve(List_models[[2]]$coefficients[1]+List_models[[2]]$coefficients[2]*x, add = TRUE)
plot(Response_2~Explanatory_1,data=DF,type="n")
curve(List_models[[3]]$coefficients[1]+List_models[[3]]$coefficients[2]*x, add = TRUE)
plot(Response_2~Explanatory_2,data=DF,type="n")
curve(List_models[[4]]$coefficients[1]+List_models[[4]]$coefficients[2]*exp(x), add = TRUE)

Thank you in advance for any help with this.

James White
  • 705
  • 2
  • 7
  • 20
  • 2
    It doesn't seem like you are plotting the coefficient values, it seems like you are plotting the fitted values (the predictions). You don't have to calculate those manually, use `model$fitted.values` to get the predictions at the input `x` values, or use `predict` on a custom set of new values to get the predictions. – Gregor Thomas Dec 10 '18 at 14:48
  • Thank you for your response Gregor. I have edited the question but I still do not now how to incorporate this within a loop or lapply function. I should also say that I am using a different stats technique and the $coefficient technique provides the equivalent of what I need from the other package (lqmm). But I thought I would keep a more general question using Base. – James White Dec 10 '18 at 14:59

1 Answers1

1

As Gregor said, you just need predict to have the predicted values. I would urge you to use ggplot, for simplicity in this kind of representations:

DF$model1 <- predict(List_models[[1]])
DF$model2 <- predict(List_models[[2]])

library(ggplot2)
ggplot(DF)+
  geom_point(aes(X,Y,color = "initial values"))+
  geom_line(aes(X,model1,color = "model1"))+
  geom_line(aes(X,model2,color = "model2"))+
  facet_wrap(~Factor)

enter image description here

If you have a lot of models, and want to loop, you ll need something like this:

for(i in 1:length(List_models))
    {DF[[paste0("model",i)]] <-  predict(List_models[[i]])}
library(data.table)
DF <- setDT(DF)

library(ggplot2)
ggplot(melt(DF,measure.vars = patterns("model")))+
  geom_point(aes(X,Y,color = "initial values"))+
  geom_line(aes(X,value,color = variable))+
  facet_wrap(~Factor)
denis
  • 5,580
  • 1
  • 13
  • 40
  • Hi denis, thank you for your response and apologies for editing the question while you were putting this answer up (I thought the original post wasn't too clear). The predict function is great, and I can use this in a lapply function [lapply(List_models, function(x) predict(x))] to do this for all models. But I do not then know how to loop through this list of predictions and plot each of the curves. [lapply(List_models,function(x) plot(x))] does not work. Do you know of a way to do this? – James White Dec 10 '18 at 15:05
  • I edited my answer to have the general case. I used here `data.table` for the melt function that I know. You could do similarly with `dplyr` – denis Dec 10 '18 at 15:12
  • Thanks again for this Denis. Again this is my fault for editing the original code so I apologize for this, but there's actually multiple Explanatory and Response variables. I still would not know how to adapt your code to account for changing parameters incorporated within each model. – James White Dec 10 '18 at 15:17