0

I have a data frame like this

X <- matrix(rexp(30, rate=.1), ncol=5)
Y <- matrix(rexp(6, rate=.1), ncol=1)
mydata <- data.frame(cbind(Y,X))

Now I want to change the third column of X with each of the columns of Xm

Xm <- matrix(rexp(60, rate=.1), ncol=10)

and do a linear regression and save the sd of X3. If I want to do it manually I do

X[ , 3] <- Xm[ , 1]
mydata <- data.frame(cbind(Y,X))
fit1 = lm(Y~.,data=mydata)

then

X[ , 3] <- Xm[ , 2]
mydata <- data.frame(cbind(Y,X))
fit2 = lm(Y~.,data=mydata)

then

X[ , 3] <- Xm[ , 3]
mydata <- data.frame(cbind(Y,X))
fit3 = lm(Y~.,data=mydata)

.
.
.

etc. 

however, if I do it manually it takes so long and not efficient. Is there anyone who can help me to make it more automatic?

nik
  • 2,500
  • 5
  • 21
  • 48

3 Answers3

1

You can do it in loop

like

res=lapply(1:ncol(Xm),function(i){
mydata[[3]] <- Xm[ , i] # change 3-rd column of mydata om i-th colomn of Xm
lm(Y~.,data=mydata)
})

where res - list of your lm models

If you want to save only X3 std you can do it in such way

res2=lapply(1:ncol(Xm),function(i){
mydata[[3]] <- Xm[ , i]
summary(lm(Y~.,data=mydata))$coefficients["X3","Std. Error"]
})

but in your example they all NaN

Batanichek
  • 7,761
  • 31
  • 49
  • thanks for your comment, would it be possible to only keep the Std. Error of the X3 for all loops as output ? if so, I accept your answer – nik Oct 19 '16 at 12:24
  • Added , but in question is nothing about it – Batanichek Oct 19 '16 at 12:33
  • please look at this one too http://stackoverflow.com/questions/40129153/how-to-randomly-exchange-a-column-within-a-function – nik Oct 19 '16 at 12:46
1

maybe with sapply?

X <- matrix(rexp(30, rate=.1), ncol=5)
Y <- matrix(rexp(6, rate=.1), ncol=1)
mydata <- data.frame(cbind(Y,X))

Xm <- matrix(rexp(60, rate=.1), ncol=10)

sapply(1:ncol(Xm), function(mycol) {
  X[ , 3] <- Xm[ , mycol]
  mydata <- data.frame(cbind(Y,X))
  fit = lm(Y~.,data=mydata) 
  return(sqrt(diag(vcov(fit))))
}, USE.NAMES = F)

UPDATE 1: Modified to extract the standard errors, according to https://stats.stackexchange.com/questions/17571/how-to-store-the-standard-errors-with-the-lm-function-in-r

Community
  • 1
  • 1
1

I like the plyr package, and you wanted the coefficients:

myfun = function(x) {
    mydata[[3]] <- x
    coef(lm(Y ~ ., data = mydata))
}

ldply(as.data.frame(Xm), myfun)
Johannes Ranke
  • 469
  • 3
  • 11
  • look at this question, if you give any solution, I will accept it http://stackoverflow.com/questions/40129153/how-to-randomly-exchange-a-column-within-a-function – nik Oct 19 '16 at 12:58