Skip to content

วิเคราะห์ ANOVA / MANOVA ด้วยโปรแกรม R

ANOVA ย่อมาจาก ANalysis OVAriance

เป้าหมาย: ทดสอบว่าความแปรปรวน (variance) ของตัวแปรตาม (dependent variable) นั้นได้รับอิทธิพลจากตัวแปรต้น (independent variable) หรือไม่

Analysis of variance

สมมติฐาน

  • y เป็นตัวแปรตามมีค่าต่อเนื่อง
  • x เป็นตัวแปรต้นมีค่าต่อเนื่อง
  • A, B เป็นตัวแปรแบบกลุ่ม ค่าไม่ต่อเนื่อง และไม่สามารถเรียงลำดับได้ (nominal data, factor)

ตัวแปรตัวอย่าง

y = ค่าใช้จ่ายนักท่องเที่ยว

A = ภูมิลำเนาของนักเท่องเที่ยว (แยกตามประเทศ หรือ ภูมิภาค)

B = อาชีพของนักท่องเที่ยว

mydataframe = ชื่อไฟล์ข้อมูล (data frame)

y และ A, B  เป็นตัวแปรที่อยู่ในไฟล์ข้อมูลชื่อ mydataframe

ขั้นตอนการวิเคราะห์ 

1. สร้างโมเดล

1.1 One-Way ANOVA


fit <- aov(y ~ A, data = mydataframe)
# y is a dependent variable (numerric)
# A is an independent factor

1.2 Two-Way ANOVA


fit <- aov(y ~ A * B, data = mydataframe)
# y is a dependent variable (numerric)
# A is an independent factor
# B is another independent factor

1.3 Analysis of Covariance


fit <- aov(y ~ A + x, data = mydataframe)
# y is a dependent variable (numerric)
# A is an independent factor
# B is another independent factor

2. ส่องกล้องมองความสัมพันธ์

par(mfrow=c(2,2)) # set layout 2 x 2
plot(fit) # diagnostic plots

3. ประเมินผล

R จะให้ค่า Type I sequential SS ซึ่งต่างจาก SAS  หรือ SPSS ที่ให้ค่า Type II marginal SS

summary(fit) # display Type I ANOVA table
drop1(fit,~.,test="F") # type III SS and F Tests

MANOVA

ใช้ในกรณีที่ตัวแปรต้นและตัวแปรตามต่างก็มีมากกว่า 1 ตัวแปร

เช่น หากเรามี 3 ตัวแปรตาม y1, y2, y3  และมีตัวแปรต้น 2 ตัวแปรคือ A และ B

# 2x2 Factorial MANOVA with 3 Dependent Variables.
Y <- cbind(y1,y2,y3) fit <- manova(Y ~ A*B)
summary(fit, test="Pillai" # or test="Wilks" or test="Hotelling-Lawley",or test"Roy"
)

คู่มือวิเคราะห์เศรษฐมิติและสถิติด้วยโปรแกรม R

4 Comments Post a comment
  1. เจเลอร์ #

    ช่วยยกตตัวอย่างคนติดโควิดให้หน่อยได้ไหมคะ เช่นในเดือนที่ผ่านมามีคนติดมาเท่าไหร่ด้วยโปรแกรมR ANOVA/MANOVAให้ดูหน่อยได้ไหมคะ🙏🏽🙏🏾🙏🏿

    October 11, 2021
    • ถ้ากรณีนี้น่าจะเป็นการวิเคราะห์ Time Series นะครับ

      October 11, 2021
      • มัมแอ๋ว #

        มีไฟล์ตัวอย่างพวกรายได้กับค่าใช้จ่ายด้วยความแปรปรวนบ้างไหมครับ

        October 12, 2021

Trackbacks & Pingbacks

  1. The 1st Year of Pairach.com and its future? | Pairach Piboonrungroj

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: