3

サブジェクトごとにグループ化された観察に lm() を適用したいのですが、sapply 構文がうまくいきません。最後に、被験者ごとに 1 行のデータフレームと、切片と勾配 (つまり、subj, lm$coefficients[1] lm$coefficients[2] の行) が必要です。

set.seed(1)
subj <- rep(c("a","b","c"), 4) # 4 observations each on 3 experimental subjects
ind <- rnorm(12) #12 random numbers, the independent variable, the x axis
dep <- rnorm(12) + .5 #12 random numbers, the dependent variable, the y axis
df <- data.frame(subj=subj, ind=ind, dep=dep)
s <- (split(df,subj)) # create a list of observations by subject

s から 1 セットの観測値を取得し、データフレームを作成して、必要なものを取得できます。

df2 <- as.data.frame(s[1])
df2
lm1 <- lm(df2$a.dep ~ df2$a.ind)

lm1$coefficients[1]
lm1$coefficients[2]

s のすべての要素をループして、データを必要な最終形式にするのに問題があります。

lm.list <- sapply(s, FUN= function(x)
  (lm(x[ ,"dep"] ~ x[,"ind"])))
a <-as.data.frame(lm.list)

以下の構造のある種の転置が必要な気がします。列 (a、b、c) は行にしたいものですが、t(a) は機能しません。

head(a)
                                                            a
coefficients                             0.1233519, 0.4610505
residuals        0.4471916, -0.3060402, 0.4460895, -0.5872409
effects          -0.6325478, 0.6332422, 0.5343949, -0.7429069
rank                                                        2
fitted.values 0.74977179, 0.09854505, -0.05843569, 0.47521446
assign                                                   0, 1
                                                             b
coefficients                              1.1220840, 0.2024222
residuals     -0.04461432, 0.02124541, 0.27103003, -0.24766112
effects           -2.0717363, 0.2228309, 0.2902311, -0.2302195
rank                                                         2
fitted.values       1.1012775, 0.8433366, 1.1100777, 1.0887808
assign                                                    0, 1
                                                         c
coefficients                          0.2982019, 0.1900459
residuals     -0.5606330, 1.0491990, 0.3908486, -0.8794147
effects       -0.6742600, 0.2271767, 1.1273566, -1.0345665
rank                                                     2
fitted.values   0.3718773, 0.2193339, 0.5072572, 0.2500516
assign                                                0, 1
4

2 に答える 2

5

それの音によって、これはあなたがやろうとしていることかもしれません:

sapply(s, FUN= function(x)
  lm(x[ ,"dep"] ~ x[,"ind"])$coefficients[c(1, 2)])
#                      a          b          c
# (Intercept) 0.71379430 -0.6817331  0.5717372
# x[, "ind"]  0.07125591  1.1452096 -1.0303726

これがあなたが探しているものである場合、他の選択肢

s/lapply一般に、分割してから を使用する場合、通常は直接ジャンプしてステップbyをスキップできることに注意してください。split

do.call(rbind, 
        by(data = df, INDICES=df$subj, FUN=function(x) 
          lm(x[, "dep"] ~ x[, "ind"])$coefficients[c(1, 2)]))
#   (Intercept)  x[, "ind"]
# a   0.7137943  0.07125591
# b  -0.6817331  1.14520962
# c   0.5717372 -1.03037257

または、「data.table」のような、より便利にそのような種類の計算を実行できるパッケージの 1 つを使用できます。

library(data.table)
DT <- data.table(df)
DT[, list(Int = lm(dep ~ ind)$coefficients[1],
          Slo = lm(dep ~ ind)$coefficients[2]), by = subj]
#    subj        Int         Slo
# 1:    a  0.7137943  0.07125591
# 2:    b -0.6817331  1.14520962
# 3:    c  0.5717372 -1.03037257
于 2013-10-26T05:40:56.127 に答える
3

どうnlme::lmListですか?

library(nlme)

coef(lmList(dep~ind|subj,df))
##   (Intercept)         ind
## a   0.7137943  0.07125591
## b  -0.6817331  1.14520962
## c   0.5717372 -1.03037257

必要に応じて、これを転置できます。

于 2013-10-26T14:05:56.377 に答える