2

単一のデータセット内で数回トレンドによって説明される分散を解決しようとして、厄介な問題に遭遇しています.....

私のデータはこのように構成されています

x <- read.table(text = "
STA YEAR    VALUE
a   1968    457
a   1970    565
a   1972    489
a   1974    500
a   1976    700
a   1978    650
a   1980    659
b   1968    457
b   1970    565
b   1972    350
b   1974    544
b   1976    678
b   1978    650
b   1980    690
c   1968    457
c   1970    565
c   1972    500
c   1974    600
c   1976    678
c   1978    670
c   1980    750 " , header = T)    

そして、私はこのようなものを返そうとしています

STA  R-sq
a    n1
b    n2
c    n3

ここで、n# は、元のセット内の位置データの対応する r-2 乗値です....

私が試してみました

fit <- lm(VALUE ~ YEAR + STA, data = x) 

個々の測点ごとに VALUE の年次傾向のモデルを提供するために、マスター データ セット内の VALUE のデータが利用可能です。

どんな助けでも大歓迎です....私はこれに本当に困惑しており、Rの問題に精通しているだけであることを知っています。

4

3 に答える 3

2

の各グループに対してVALUE~のr-squared を取得するには、この前の回答を使用して、わずかに変更し、値をプラグインします。YEARSTA

# assuming x is your data frame (make sure you don't have Hmisc loaded, it will interfere)
models_x <- dlply(x, "STA", function(df) 
     summary(lm(VALUE ~ YEAR, data = df)))

# extract the r.squared values
rsqds <- ldply(1:length(models_x), function(x) models_x[[x]]$r.squared)
# give names to rows and col
rownames(rsqds) <- unique(x$STA)
colnames(rsqds) <- "rsq"
# have a look
rsqds
        rsq
a 0.6286064
b 0.5450413
c 0.8806604

編集: ここで mnel の提案に従うと、r-2 乗値を適切なテーブルに取得するより効率的な方法があります (行と列の名前を追加する必要はありません)。

# starting with models_x from above
rsqds <- data.frame(rsq =sapply(models_x, '[[', 'r.squared'))

# starting with just the original data in x, this is great:
rsqds  <- ddply(x, "STA", summarize, rsq = summary(lm(VALUE ~ YEAR))$r.squared)

  STA       rsq
1   a 0.6286064
2   b 0.5450413
3   c 0.8806604
于 2013-02-05T22:56:06.657 に答える
0

r二乗値は3つではなく1つだけです..質問を編集してください

# store the output 
y <- summary( lm( VALUE ~ YEAR + STA , data = x ) )
# access the attributes of `y`
attributes( y )
y$r.squared
y$adj.r.squared
y$coefficients
y$coefficients[,1]

# or are you looking to run three separate
# lm() functions on 'a' 'b' and 'c' ..where this would be the first? 
y <- summary( lm( VALUE ~ YEAR , data = x[ x$STA %in% 'a' , ] ) )
# access the attributes of `y`
attributes( y )
y$r.squared
y$adj.r.squared
y$coefficients
y$coefficients[,1]
于 2013-02-05T21:14:42.957 に答える