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

Bert Gunter gunter.berton at gene.com
Fri Nov 25 13:04:17 CET 2011

Inline below.

-- Bert

On Fri, Nov 25, 2011 at 2:31 AM, Milan Bouchet-Valat <nalimilan at 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.

Actually an array **is** a vector -- but with an additional "dim"
attribute. Try:

>> 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])

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

(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-help at 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

More information about the R-help mailing list