2011-10-23 35 views
9

我正在閱讀Cohen,Cohen,Aiken和West(2003)的一本書「行爲科學的應用多元迴歸相關分析」,並且遇到了迴歸曲面的3d圖,顯示了相互作用和沒有相互作用第259頁)。這些圖看起來像是可能使用R創建的。我喜歡將這些圖作爲教學工具並希望重現它們。情節看起來是這樣的: enter image description herePlot Regression Surface

唯一除了科恩等人。平均值爲平面上的曲線,x2爲平均值+ 1sd和= 1sd。如果可能的話,這將是一個很好的補充(通常大部分事情都可以用R)

我提供了一個樣本數據集,下面有一個IV,2個預測變量和居中預測變量。我將如何使用R生成顯示交互的迴歸曲面(平面)圖和居中和未中心數據的加法模型(我假設該技術將是相同的但想要確定)。

的4個地塊總: 1.非中心沒有互動 2.非中心交互 3.中心沒有互動 4.中心互動

DF<-structure(list(y = c(-1.22, -1.73, -2.64, -2.44, -1.11, 2.24, 
3.42, 0.67, 0.59, -0.61, -10.77, 0.93, -8.6, -6.99, -0.12, -2.29, 
-5.16, -3.35, -3.35, -2.51, 2.21, -1.18, -5.21, -7.74, -1.34), 
    x1 = c(39.5, 41, 34, 30.5, 31.5, 30, 41.5, 24, 43, 39, 25.5, 
    38.5, 33.5, 30, 41, 31, 25, 37, 37.5, 24.5, 38, 37, 41, 37, 
    36), x2 = c(61L, 53L, 53L, 44L, 49L, 44L, 57L, 47L, 54L, 
    48L, 46L, 59L, 46L, 61L, 55L, 57L, 59L, 59L, 55L, 50L, 62L, 
    55L, 55L, 52L, 55L), centered.x1 = c(5.49702380952381, 6.99702380952381, 
    -0.0029761904761898, -3.50297619047619, -2.50297619047619, 
    -4.00297619047619, 7.49702380952381, -10.0029761904762, 8.99702380952381, 
    4.99702380952381, -8.50297619047619, 4.49702380952381, -0.50297619047619, 
    -4.00297619047619, 6.99702380952381, -3.00297619047619, -9.00297619047619, 
    2.99702380952381, 3.49702380952381, -9.50297619047619, 3.99702380952381, 
    2.99702380952381, 6.99702380952381, 2.99702380952381, 1.99702380952381 
    ), centered.x2 = c(9.80357142857143, 1.80357142857143, 1.80357142857143, 
    -7.19642857142857, -2.19642857142857, -7.19642857142857, 
    5.80357142857143, -4.19642857142857, 2.80357142857143, -3.19642857142857, 
    -5.19642857142857, 7.80357142857143, -5.19642857142857, 9.80357142857143, 
    3.80357142857143, 5.80357142857143, 7.80357142857143, 7.80357142857143, 
    3.80357142857143, -1.19642857142857, 10.8035714285714, 3.80357142857143, 
    3.80357142857143, 0.803571428571431, 3.80357142857143)), .Names = c("y", 
"x1", "x2", "centered.x1", "centered.x2"), row.names = c(NA, 
25L), class = "data.frame") 

預先感謝您。

編輯:下面的代碼繪製飛機,但不會爲你有交互(這真的是我感興趣)的工作。另外,我不知道如何繪製x2的高(+ 1sd),低(-1sd)和平均值。

x11(10,5) 
s3d <- scatterplot3d(DF[,c(2,3,1)], type="n", highlight.3d=TRUE, 
     angle=70, scale.y=1, pch=16, main="scatterplot3d") 

    # Now adding a regression plane to the "scatterplot3d" 
    my.lm <- with(DF, lm(y ~ x1 + x2)) 
s3d$plane3d(my.lm, lty.box = "solid") 

試圖繪製的交互平面(這裏看到的):

s3d <- scatterplot3d(DF[,c(2,3,1)], type="n", highlight.3d=TRUE, 
     angle=70, scale.y=1, pch=16, main="scatterplot3d") 

    my.lm <- with(DF, lm(y ~ x1 + x2 + x1:x2)) 
s3d$plane3d(my.lm, lty.box = "solid") 

產生了以下錯誤:

Error in segments(x, z1, x + y.max * yx.f, z2 + yz.f * y.max, lty = ltya, : 
    cannot mix zero-length and non-zero-length coordinates 
+0

我覺得有可能是一些R中指揮官,做這樣的事情... –

回答

13

這是我會怎麼做(平添幾分色彩)包'rms'和'格':

require(rms) # also need to have Hmisc installed 
require(lattice) 
ddI <- datadist(DF) 
options(datadist="ddI") 
lininterp <- ols(y ~ x1*x2, data=DF) 
bplot(Predict(lininterp, x1=25:40, x2=45:60), 
     lfun=wireframe, # bplot passes extra arguments to wireframe 
     screen = list(z = -10, x = -50), drape=TRUE) 

enter image description here

和非交互模型:

bplot(Predict(lin.no.int, x1=25:40, x2=45:60), lfun=wireframe, col=2:8, drape=TRUE, 
screen = list(z = -10, x = -50), 
main="Estimated regression surface with no interaction") 

enter image description here