列出lm对象,保留其类 [英] make a list of lm objects, retain their class

查看:96
本文介绍了列出lm对象,保留其类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这样一个基本问题的道歉-我肯定想念一些明显的东西.

我想构建一个lm对象的列表,然后将在llply调用中使用该对象以对此列表执行中介分析.但这并不重要-我只想首先列出长度为m的列表(其中m是模型集),并且m内的每个元素本身将包含n个lm对象.

所以在这个简单的例子中

d1 <- data.frame(x1 = runif(100, 0, 1),
             x2 = runif(100, 0, 1),
             x3 = runif(100, 0, 1),
             y1 = runif(100, 0, 1),
             y2 = runif(100, 0, 1),
             y3 = runif(100, 0, 1))

m1 <- lm(y1 ~ x1 + x2 + x3, data = d1)
m2 <- lm(x1 ~ x2 + x3, data = d1)
m3 <- lm(y2 ~ x1 + x2 + x3, data = d1)
m4 <- lm(x2 ~ x1 + x3, data = d1)
m5 <- lm(y3 ~ y1 + y2 + x3, data = d1)
m6 <- lm(x3 ~ x1 + x2, data = d1)

我想要一个包含3个元素的列表,第一个元素将包含m1m2,第二个元素将包含m3m4,依此类推.我最初的尝试是正确的,但是lmm对象不会保留其类.

mlist <- list(c(m1,m2),
              c(m3,m4),
              c(m5,m6))

它的长度正确(即length(mlist)等于3),但是我认为我可以使用

d1 <- data.frame(x1 = runif(100, 0, 1),
             x2 = runif(100, 0, 1),
             x3 = runif(100, 0, 1),
             y1 = runif(100, 0, 1),
             y2 = runif(100, 0, 1),
             y3 = runif(100, 0, 1))

m1 <- lm(y1 ~ x1 + x2 + x3, data = d1)
m2 <- lm(x1 ~ x2 + x3, data = d1)
m3 <- lm(y2 ~ x1 + x2 + x3, data = d1)
m4 <- lm(x2 ~ x1 + x3, data = d1)
m5 <- lm(y3 ~ y1 + y2 + x3, data = d1)
m6 <- lm(x3 ~ x1 + x2, data = d1)

对象本身来访问

class(mlist[1][[1]])

但是这个元素显然是一个列表.

我是不是在第一步中搞砸了如何建立列表,或者这是关于lm对象的更根本的东西?

解决方案

不,您只是对c和列表索引感到困惑.试试这个:

mlist <- list(list(m1,m2),
              list(m3,m4),
              list(m5,m6))
> class(mlist[[1]][[1]])
[1] "lm"

因此,c将通过拼合列表来串联它们.对于lm对象,基本上意味着它是将每个对象组件列表中的每个lm对象展平,然后将所有这些列表串联在一起. c更直观地用于原子矢量.

列表索引经常使人们绊倒.要记住的是,[总是返回子列表,而[[选择一个元素.

在上面的示例中,这意味着mlist[1]将返回长度为1的列表.第一个元素仍然是一个列表.因此,您必须执行mlist[1][[1]][[1]]之类的方法,才能一直沿这种方式到达lm对象.

Apologies for such a rudimentary question--I must be missing something obvious.

I want to build a list of lm objects, which I'm then going to use in an llply call to perform mediation analysis on this list. But this is immaterial--I just first want to make a list of length m (where m is the set of models) and each element within m will itself contain n lm objects.

So in this simple example

d1 <- data.frame(x1 = runif(100, 0, 1),
             x2 = runif(100, 0, 1),
             x3 = runif(100, 0, 1),
             y1 = runif(100, 0, 1),
             y2 = runif(100, 0, 1),
             y3 = runif(100, 0, 1))

m1 <- lm(y1 ~ x1 + x2 + x3, data = d1)
m2 <- lm(x1 ~ x2 + x3, data = d1)
m3 <- lm(y2 ~ x1 + x2 + x3, data = d1)
m4 <- lm(x2 ~ x1 + x3, data = d1)
m5 <- lm(y3 ~ y1 + y2 + x3, data = d1)
m6 <- lm(x3 ~ x1 + x2, data = d1)

I want a list containing 3 elements, and the first element will contain m1 and m2, the second will contain m3 and m4, etc. My initial attempt is sort of right, but the lmm objects don't retain their class.

mlist <- list(c(m1,m2),
              c(m3,m4),
              c(m5,m6))

It has the right length (ie length(mlist) equals 3), but I thought I could access the lm object itself with

class(mlist[1][[1]])

but this element is apparently a list.

Am I screwing up how I build the list in the first step, or is this something more fundamental regarding lm objects?

解决方案

No, you're just getting confused with c and list indexing. Try this:

mlist <- list(list(m1,m2),
              list(m3,m4),
              list(m5,m6))
> class(mlist[[1]][[1]])
[1] "lm"

So c will concatenate lists by flattening them. In the case of a lm object, that basically means it's flattening each lm object in a list of each of the object components, and then concatenating all those lists together. c is more intuitively used on atomic vectors.

The indexing of lists often trips people up. The thing to remember is that [ will always return a sub-list, while [[ selects an element.

In my example above, this means that mlist[1] will return a list of length one. That first element is still a list. So you'd have to do something like mlist[1][[1]][[1]] to get all the way down to the lm object that way.

这篇关于列出lm对象,保留其类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆