NickDanks
NickDanks

Reputation: 25

Running lm() on user defined matrix in R

I am trying to set up a function that will run lm() on a model derived from a user defined matrix in R.

The modelMatrix would be set up by the user, but would be expected to be structured as follows: source target 1 "PVC" "AA" 2 "Aro" "AA" 3 "PVC" "Aro" This matrix serves to allow the user to define the dependent(target) and independent(source) variables in the lm and refers to the column names of another valuesMatrix:
PVC Ar AA [1,] -2.677875504 0.76141471 0.006114699 [2,] 0.330537781 -0.18462039 -0.265710261 [3,] 0.609826160 -0.62470233 0.715474554

I need to take this modelMatrix and generate a relevant number of lms. Eg in this case:
lm(AA ~ PVC + Aro) and
lm(Aro ~ PVC)
I tried this, but it would seem that as the user changes the model matrix, it becomes erroneous and I need to explicitly specify each independent variable according to the modelMatrix.

```lm(as.formula(paste(unique(modelMatrix[,"target"])[1], "~ .",sep="")),
       data=data.frame(valuesMatrix))
```

Do I need to set up 2 loops (1 nested) to fetch the source and target strings and paste them into the formula or am I overlooking some detail. Very confused.

Ideally I would like the user to be able to change the modelMatrix to include 1 or many lms and one or many independent variables for each lm. I would really appreciate your assistance as I am really hitting a wall here. Thanks.

Upvotes: 1

Views: 565

Answers (1)

RDizzl3
RDizzl3

Reputation: 318

For your specific example this code should work -

source <- c("PVC","Aro","PVC")
target <- c("AA","AA","Aro")

modelMatrix <- data.frame(source = source, target = target)

valuesMatrix <- as.matrix(rbind(c(-2.677875504,0.76141471,0.006114699), c(0.330537781,-0.18462039,-0.265710261), 
                      c(0.609826160,-0.62470233,0.715474554)))

colnames(valuesMatrix) <- c("PVC","Aro","AA")

unique.target <- as.character(unique(modelMatrix$target))

lm.models <- lapply(unique.target, function(x) {lm(as.formula(paste(x,"~ .", sep = "")), 
      data = data.frame(valuesMatrix[,colnames(valuesMatrix) %in% 
                                       c(x,as.character(modelMatrix$source[which(modelMatrix$target==x)]))]))})

You can use the ideas of the for loop but for loops can be to expensive especially if your modelMatrix get really large. It might not look as appealing but the lapply function is optimized for this type of job. The only other trick to this was to keep the columns required to perform the lm.

You can pull the results of each lm also but using:

lm[[1]] and lm[[2]]

Upvotes: 1

Related Questions