簡體   English   中英

計算R中各組之間平方和的公式

[英]Formula to compute the between group sum of squares in R

誰能告訴我如何編碼R之間的SS以便手工計算,它是∑ ni(meanXi-均值)2

謝謝,lp

如果您有一個向量xx_mean的平均值, x_mean可以像下面這樣手動計算SS誤差:

> x=c(1,2,3,4,5)
> x_mean = mean(x)
> x-x_mean
[1] -2 -1  0  1  2
> (x-x_mean)^2
[1] 4 1 0 1 4
> sum((x-x_mean)^2)
[1] 10

不確定這是您想要的,但是

#  create sample dataset: 5 groups, 10 values per group
set.seed(1)
df   <- data.frame(group=rep(LETTERS[1:5],each=10),value=rnorm(50))
# calculate between-group sum of squares (SSB)
sum((aggregate(value~group,df,mean)$value-mean(df$value))^2)
# [1] 0.07938908

這將使用aggregate(...)按組計算平均值,然后求和該平均值與總體平均值( mean(df$value) )的平方差之和。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM