[R] Error: invalid type(list) for variable when using lm()

1,503 views
Skip to first unread message

Tianchan Niu

unread,
Nov 16, 2010, 2:07:34 PM11/16/10
to R-h...@r-project.org
Dear All,I would like to do multiple regression in R. I used: lm(y~X),
where y is a n by 1 vector, and X is a n by m matrix. I kept getting the error
message:Error in model.frame.default(formula = y ~ X,  :  invalid type (list) for variable 'X'. However, when I used: lm(y~X[,1]+X[,2]+X[,3]+…+X[,m]), it
works well, but this is not the form I prefer, it makes my codes complicated.Please help.Thank you very much,Jane

Henrique Dallazuanna

unread,
Nov 16, 2010, 2:03:56 PM11/16/10
to Tianchan Niu, R-h...@r-project.org
Try this:

y <- rnorm(100)
X <- matrix(runif(100 * 10), ncol = 10)

lm(y ~ ., data = cbind.data.frame(y, X))

> ______________________________________________
> R-h...@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

[[alternative HTML version deleted]]

Phil Spector

unread,
Nov 16, 2010, 2:04:48 PM11/16/10
to Tianchan Niu, R-h...@r-project.org
Tianchan -
Your X is not a matrix -- it's a dataframe. Probably the
simplest solution is to use

lm(y~as.matrix(X))

but you should also learn the difference between a data frame
and a matrix.

- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spe...@stat.berkeley.edu

Dhaynes

unread,
Nov 25, 2011, 3:02:51 AM11/25/11
to r-h...@r-project.org
Hello,

I am new to R.
I have multidimensional array (379,2,3) and I need to create a series of
linear regressions (379 to be exact)
I have the array stored properly I believe, but I can not use the
lm(myarray[1,1,1:3]~myarray[1,2,1:3])
I have checked to make sure they are exactly the same length.
I have also tried endlessly to convert the subset of the array back into a
vector.

any help would be appreciated.

--
View this message in context: http://r.789695.n4.nabble.com/Error-invalid-type-list-for-variable-when-using-lm-tp3045462p4106669.html
Sent from the R help mailing list archive at Nabble.com.

Milan Bouchet-Valat

unread,
Nov 25, 2011, 5:31:47 AM11/25/11
to Dhaynes, r-h...@r-project.org
Le vendredi 25 novembre 2011 à 00:02 -0800, Dhaynes a écrit :
> Hello,
>
> I am new to R.
> I have multidimensional array (379,2,3) and I need to create a series of
> linear regressions (379 to be exact)
> I have the array stored properly I believe, but I can not use the
> lm(myarray[1,1,1:3]~myarray[1,2,1:3])
> I have checked to make sure they are exactly the same length.
> I have also tried endlessly to convert the subset of the array back into a
> vector.
>
> any help would be appreciated.
The 'formula' argument of lm doesn't take actual values, but variable
names. So you need to create vectors containing your data, or pass a
data frame with these vectors are columns. So, going the latter way :
df <- data.frame(a=myarray[1,1,1:3], b=myarray[1,2,1:3])
lm(a ~ b, data=df)

or in one step
lm(a ~ b, data=data.frame(a=myarray[1,1,1:3], b=myarray[1,2,1:3]))


Regards

Bert Gunter

unread,
Nov 25, 2011, 7:04:17 AM11/25/11
to Milan Bouchet-Valat, r-h...@r-project.org
Inline below.

-- Bert

On Fri, Nov 25, 2011 at 2:31 AM, Milan Bouchet-Valat <nali...@club.fr> wrote:
> Le vendredi 25 novembre 2011 à 00:02 -0800, Dhaynes a écrit :
>> Hello,
>>
>> I am new to R.
>> I have multidimensional array (379,2,3) and I need to create a series of
>> linear regressions (379 to be exact)
>> I have the array stored properly I believe, but I can not use the
>> lm(myarray[1,1,1:3]~myarray[1,2,1:3])
>> I have checked to make sure they are exactly the same length.
>> I have also tried endlessly to convert the subset of the array back into a
>> vector.

?as.vector
Actually an array **is** a vector -- but with an additional "dim"
attribute. Try:
>str(x)


>>
>> any help would be appreciated.

1) Read relevant portions of R docs, like ?array and perhaps "An
Introduction to R."

2) Read and follow the posting guide. In particular, give us a toy
example with the code you used to construct your array. It's difficult
to diagnose the source of engine failure without the car.

3) See my comment below.

> The 'formula' argument of lm doesn't take actual values, but variable
> names. So you need to create vectors containing your data, or pass a

--This is patently false. Please check before giving obviously wrong advice:

> x <- array(rnorm(150), dim= c(10,5,3))
> lm(x[,3,2] ~x[,1,1])

Call:
lm(formula = x[, 3, 2] ~ x[, 1, 1])

Coefficients:
(Intercept) x[, 1, 1]
-0.1247 0.1171

> data frame with these vectors are columns. So, going the latter way :
> df <- data.frame(a=myarray[1,1,1:3], b=myarray[1,2,1:3])
> lm(a ~ b, data=df)
>
> or in one step
> lm(a ~ b, data=data.frame(a=myarray[1,1,1:3], b=myarray[1,2,1:3]))
>
>
> Regards
>
> ______________________________________________
> R-h...@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

Dhaynes

unread,
Nov 25, 2011, 11:41:19 AM11/25/11
to r-h...@r-project.org
Ok let me clarify

I have multidimensional array and I need to convert it to a singular
dimensional array.
The multidimensional array is 359 rows, 2 cols, 3 deep
I need to run a regression model mymatrix[1,1,1:3] and mymatrix [1,2,1:3]

This is my current error, which indicates I have the incorrect list type (I
have tried functions as.list, as.vector, as.vector)

lm(formula = mymatrix[1,1,1:3]~mymatrix[1,2,1:3] )
Error in model.frame.default(formula = mymatrix[1, 1, 1:3] ~ mymatrix[1, :
invalid type (list) for variable 'mymatrix[1, 1, 1:3]'


I was unsuccessful at attempting the "str(mymatrix[1,1,1:3]" --Argument not
valid model

The data.frame function did not create the objects
<- data.frame(a=mymatrix[1,1,1:3], b=mymatrix[1,2,1:3])
> lm(a~b, data=df)
Error in eval(expr, envir, enclos) : object 'a' not found

Here is my code
con <- dbConnect(PostgreSQL(), user="postgres",
password="antione",dbname="Education")
rs <- dbGetQuery(con, "SELECT (GRADE1[10]) As grade1_t1, (GRADE1[11]) As
grade1_t2, (GRADE1[12]) As grade1_t3, (GRADE2[11]) As grade2_t2,
(GRADE2[12]) As grade2_t3, (GRADE2[13]) As grade2_t4 FROM attending")
myval <- rs
attach(myval)
names(myval)
dim(myval)

mymatrix <- array(myval, c(379,2,3))

mymatrix[,1,1] <- grade1_t1
mymatrix[,1,2] <- grade1_t2
mymatrix[,1,3] <- grade1_t3
mymatrix[,2,1] <- grade2_t2
mymatrix[,2,2] <- grade2_t3
mymatrix[,2,3] <- grade2_t4

I can do this
plot(mymatrix[1,1,1:3],mymatrix[1,2,1:3])

On Fri, Nov 25, 2011 at 6:06 AM, Bert Gunter [via R] <
ml-node+s7896...@n4.nabble.com> wrote:

> Inline below.
>
> -- Bert
>
> On Fri, Nov 25, 2011 at 2:31 AM, Milan Bouchet-Valat <[hidden email]<http://user/SendEmail.jtp?type=node&node=4107159&i=0>>

> > [hidden email] <http://user/SendEmail.jtp?type=node&node=4107159&i=1>mailing list


> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
>
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
> ______________________________________________

> [hidden email] <http://user/SendEmail.jtp?type=node&node=4107159&i=2>mailing list


> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://r.789695.n4.nabble.com/Error-invalid-type-list-for-variable-when-using-lm-tp3045462p4107159.html
> To unsubscribe from Error: invalid type(list) for variable when using
> lm(), click here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3045462&code=aGF5bmVzZDJAZ21haWwuY29tfDMwNDU0NjJ8MjA1OTM1OTY5>
> .
> NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.InstantMailNamespace&breadcrumbs=instant+emails%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>


--
View this message in context: http://r.789695.n4.nabble.com/Error-invalid-type-list-for-variable-when-using-lm-tp3045462p4107912.html


Sent from the R help mailing list archive at Nabble.com.

[[alternative HTML version deleted]]

David Winsemius

unread,
Nov 25, 2011, 1:10:59 PM11/25/11
to Dhaynes, r-h...@r-project.org

Generally a bad idea to attach objects. It's a sin that is committed
by several authors but it generally gets in the way of safe code
writing. Better to use with().

> names(myval)
> dim(myval)
>
> mymatrix <- array(myval, c(379,2,3))
>
> mymatrix[,1,1] <- grade1_t1
> mymatrix[,1,2] <- grade1_t2
> mymatrix[,1,3] <- grade1_t3
> mymatrix[,2,1] <- grade2_t2
> mymatrix[,2,2] <- grade2_t3
> mymatrix[,2,3] <- grade2_t4

But what are these various grade-named objects? Are you sure you
didn't coerce the matrix to character mode? What is str(mymatrix)
after this?


--
David.

> ______________________________________________
> R-h...@r-project.org mailing list


> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT

______________________________________________
R-h...@r-project.org mailing list

Bert Gunter

unread,
Nov 25, 2011, 2:53:35 PM11/25/11
to David Winsemius, r-h...@r-project.org
Inline below. HOWEVER -- my comments are tentative and need
verification by someone more expert because:

1. This is not a reproducible example, so I have no idea what really happening

2. I don't know what your dbQuery command does.Do you?

But see below for my guesses

-- Bert

On Fri, Nov 25, 2011 at 10:10 AM, David Winsemius
<dwins...@comcast.net> wrote:
>
> On Nov 25, 2011, at 11:41 AM, Dhaynes wrote:
>
>> Ok let me clarify
>>
>> I have multidimensional array and I need to convert it to a singular
>> dimensional array.
>> The multidimensional array is 359 rows, 2 cols, 3 deep
>> I need to run a regression model mymatrix[1,1,1:3] and mymatrix [1,2,1:3]
>>
>> This is my current error, which indicates I have the incorrect list type
>> (I
>> have tried functions as.list, as.vector, as.vector)
>>
>> lm(formula = mymatrix[1,1,1:3]~mymatrix[1,2,1:3] )
>> Error in model.frame.default(formula = mymatrix[1, 1, 1:3] ~ mymatrix[1,
>>  :
>>  invalid type (list) for variable 'mymatrix[1, 1, 1:3]'
>>
>>
>> I was unsuccessful at attempting the "str(mymatrix[1,1,1:3]" --Argument
>> not
>> valid model

It should be str(mymatrix)


>>
>> The data.frame function did not create the objects
>> <- data.frame(a=mymatrix[1,1,1:3], b=mymatrix[1,2,1:3])

LHS is missing, but presumably just a typo here. Note that a and b
would contain only 3 values each, presumably not what you want. And,
as I said in my earlier message, you don't need to do this anyway.

>>>
>>> lm(a~b, data=df)
>>
>> Error in eval(expr, envir, enclos) : object 'a' not found
>>
>> Here is my code
>> con <- dbConnect(PostgreSQL(), user="postgres",
>> password="antione",dbname="Education")
>> rs <- dbGetQuery(con, "SELECT (GRADE1[10]) As grade1_t1, (GRADE1[11]) As
>> grade1_t2, (GRADE1[12]) As grade1_t3, (GRADE2[11]) As grade2_t2,
>> (GRADE2[12]) As grade2_t3, (GRADE2[13]) As grade2_t4 FROM attending")

I think the problem is the structure of rs. Is it a data.frame or a
list or what? What does str(rs) give you?

I think you need to **carefully** read ?dbGetQuery


>> myval <- rs
>> attach(myval)
>
> Generally a bad idea to attach objects. It's a sin that is committed by
> several authors but it generally gets in the way of safe code writing.
> Better to use with().

-- I second this.

>>>> <http://user/SendEmail.jtp?type=node&node=4107159&i=1>mailing list


>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>
>>> http://www.R-project.org/posting-guide.html
>>>>
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>>
>>>
>>> --
>>>
>>> Bert Gunter
>>> Genentech Nonclinical Biostatistics
>>>
>>> Internal Contact Info:
>>> Phone: 467-7374
>>> Website:
>>>
>>>
>>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>>>
>>> ______________________________________________
>>> [hidden email]

>>> <http://user/SendEmail.jtp?type=node&node=4107159&i=2>mailing list

--

Bert Gunter
Genentech Nonclinical Biostatistics

______________________________________________

Reply all
Reply to author
Forward
0 new messages