Skip to content

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

6 messages · Dhaynes, Milan Bouchet-Valat, David Winsemius +1 more

#
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.
#
Le vendredi 25 novembre 2011 ? 00:02 -0800, Dhaynes a ?crit :
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
#
Inline below.

-- Bert
On Fri, Nov 25, 2011 at 2:31 AM, Milan Bouchet-Valat <nalimilan at club.fr> wrote:
?as.vector
Actually an array **is** a vector -- but with an additional "dim"
attribute. Try:
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.
--This is patently false. Please check before giving obviously wrong advice:
Call:
lm(formula = x[, 3, 2] ~ x[, 1, 1])

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

  
    
#
On Nov 25, 2011, at 11:41 AM, Dhaynes wrote:

            
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().
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?
#
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
<dwinsemius at comcast.net> wrote:
It should be str(mymatrix)
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.
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
-- I second this.