आर

2012-10-02 26 views
7

में अनुमानित मॉडल, डेटा और अवशेषों को दिखाते हुए ग्राफ कैसे बनाएं, दो चर, x और y दिए गए, मैं चर पर एक dynlm प्रतिगमन चलाता हूं और एक चर के बाद फिट मॉडल को अवशोषित करना चाहता हूं और अवशिष्ट नीचे दिखाया गया है कि वास्तविक डेटा लाइन अनुमानित रेखा से अलग कैसे होती है। मैंने इसे पहले देखा है और मैंने इसे पहले किया है, लेकिन मेरे जीवन के लिए मुझे याद नहीं है कि इसे कैसे किया जाए या जो कुछ भी समझा जाए उसे ढूंढें।आर

यह मुझे उस ballpark में ले जाता है जहां मेरे पास एक मॉडल और दो चर होते हैं, लेकिन मुझे इच्छित ग्राफ का प्रकार नहीं मिल सकता है।

library(dynlm) 
x <- rnorm(100) 
y <- rnorm(100) 
model <- dynlm(x ~ y) 

plot(x, type="l", col="red") 
lines(y, type="l", col="blue") 

मैं एक ग्राफ कि इस तरह दिखता है जहां मॉडल और वास्तविक डेटा एक दूसरे को और अवशिष्ट तल पर एक अलग ग्राफ दिखा के रूप में साजिश रची डालने देखना उत्पन्न करना चाहते हैं कि कैसे वास्तविक डेटा और मॉडल विचलित। The Objective

+0

मेरी इच्छा है कि मैं दोनों जवाब के रूप में चुन सकूं। वे दोनों जो मुझे करने की ज़रूरत है उसे खींचते हैं। मैं रिकार्डो के जवाब के साथ पूरी तरह से जाने जा रहा हूं क्योंकि यह आत्मविश्वास बाध्यकारी बक्से जोड़ता है। – FloppyDisk

उत्तर

9

इस चाल करना चाहिए:

library(dynlm) 
set.seed(771104) 
x <- 5 + seq(1, 10, len=100) + rnorm(100) 
y <- x + rnorm(100) 
model <- dynlm(x ~ y) 

par(oma=c(1,1,1,2)) 
plotModel(x, model) # works with models which accept 'predict' and 'residuals' 

और इस plotModel के लिए कोड है,

plotModel = function(x, model) { 
    ymodel1 = range(x, fitted(model), na.rm=TRUE) 
    ymodel2 = c(2*ymodel1[1]-ymodel1[2], ymodel1[2]) 
    yres1 = range(residuals(model), na.rm=TRUE) 
    yres2 = c(yres1[1], 2*yres1[2]-yres1[1]) 
    plot(x, type="l", col="red", lwd=2, ylim=ymodel2, axes=FALSE, 
     ylab="", xlab="") 
    axis(1) 
    mtext("residuals", 1, adj=0.5, line=2.5) 
    axis(2, at=pretty(ymodel1)) 
    mtext("observed/modeled", 2, adj=0.75, line=2.5) 
    lines(fitted(model), col="green", lwd=2) 
    par(new=TRUE) 
    plot(residuals(model), col="blue", type="l", ylim=yres2, axes=FALSE, 
     ylab="", xlab="") 
    axis(4, at=pretty(yres1)) 
    mtext("residuals", 4, adj=0.25, line=2.5) 
    abline(h=quantile(residuals(model), probs=c(0.1,0.9)), lty=2, col="gray") 
    abline(h=0) 
    box() 
} 

enter image description here

7

जो आप खोज रहे हैं resid(model) है। इस प्रयास करें:

library(dynlm) 
x <- 10+rnorm(100) 
y <- 10+rnorm(100) 
model <- dynlm(x ~ y) 

plot(x, type="l", col="red", ylim=c(min(c(x,y,resid(model))), max(c(x,y,resid(model))))) 
lines(y, type="l", col="green") 
lines(resid(model), type="l", col="blue") 

enter image description here

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^