[R] Question for LM intercept

Gavin Simpson gavin.simpson at ucl.ac.uk
Thu Jul 13 18:24:44 CEST 2006


On Thu, 2006-07-13 at 15:02 +0100, Pavlidis, Efthymios wrote:
> Hello,
> 
> I am having the following silly problem with lm. 
> 
> Let X be a dataframe with  X[,1] the dependent variable and X[,-1] the independent variables. I want to run the following
> but without including an intercept.
> 
> for(i in 1:100 ){
>     lm( X[,100-i] )  # this works fine but it returns an intercept
> }
> 
> Can anyone help me? Thank you in advance!

Are you really sure that works? Looks like the last loop will be
indexing column 0 and this does fail for me but for a different reason
first:

dat <- data.frame(matrix(rnorm(10000), ncol = 100))
for(i in 1:100)
    lm(dat[, 101-i])

Gives this error:
Error in terms.default(formula, data = data) :
        no terms component

I'm not exactly sure what you want to do, but if you mean you want to
fit a model to your y (X[,1]) using a single column at a time from the
rest of X, then does this do what you want?

mods <- vector("list", length = ncol(dat))
for(i in seq(along = mods))
    mods[[i]] <- lm(X1 ~ . -1, data = dat[, c(1, i)])

The -1 in the formular removes the intercept. To get the coefficients:

sapply(mods, coef)

Even then though, the first iteration of the loop is fitting a model of
X1 ~ X1 and gets a coefficient of 1:

> mods[[1]]

Call:
lm(formula = X1 ~ . - 1, data = dat[, c(1, i)])

Coefficients:
X1.1
   1

If you meant to only regress X1 on the remaining 99 variables then you
need something like this:

dat <- data.frame(matrix(rnorm(10000), ncol = 100))
## list to hold the results
mods <- vector("list", length = ncol(dat) - 1)
## indexer for the list
ind <- c(1, seq(2, ncol(dat)-1))
for(i in seq(2, ncol(dat)))  {
    j <- ind[i-1]
    mods[[j]] <- lm(X1 ~ . -1, data = dat[, c(1, i)])
}
## return the coefficients
sapply(mods, coef)

Of course it would be easier if you subset X first in that case

dat <- data.frame(matrix(rnorm(10000), ncol = 100))
X1 <- dat[, 1] # dependent variables
dat <- dat[, -1] # predictors
## list to hold the results
mods <- vector("list", length = ncol(dat))
for(i in seq(along = mods))
    mods[[i]] <- lm(X1 ~ . -1, data = dat[, i, drop = FALSE])
## return the coefficients
sapply(mods, coef)

HTH

G

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Gavin Simpson                 [t] +44 (0)20 7679 0522
 ECRC & ENSIS, UCL Geography,  [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/cv/
 London, UK. WC1E 6BT.         [w] http://www.ucl.ac.uk/~ucfagls/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list