2010-04-14 10 views
15

में कम से कम वर्गों प्रतिगमन साजिश मैं के रूप में ग्राफिक ऑफसेट सीधा कहा जाता है में यहाँ सचित्र एक कम से कम वर्गों प्रतिगमन लाइन और रेखा खंड प्रतिगमन लाइन के लिए datapoints जोड़ने के साथ एक साजिश बनाने में दिलचस्पी रहा हूँ में सीधा ऑफसेट: http://mathworld.wolfram.com/LeastSquaresFitting.html alt text http://mathworld.wolfram.com/images/eps-gif/LeastSquaresOffsets_1000.gifग्राफ़िंग आर

मैं साजिश और प्रतिगमन लाइन यहाँ किया है:

## Dataset from http://www.apsnet.org/education/advancedplantpath/topics/RModules/doc1/04_Linear_regression.html 

## Disease severity as a function of temperature 

# Response variable, disease severity 
diseasesev<-c(1.9,3.1,3.3,4.8,5.3,6.1,6.4,7.6,9.8,12.4) 

# Predictor variable, (Centigrade) 
temperature<-c(2,1,5,5,20,20,23,10,30,25) 

## For convenience, the data may be formatted into a dataframe 
severity <- as.data.frame(cbind(diseasesev,temperature)) 

## Fit a linear model for the data and summarize the output from function lm() 
severity.lm <- lm(diseasesev~temperature,data=severity) 

# Take a look at the data 
plot(
diseasesev~temperature, 
     data=severity, 
     xlab="Temperature", 
     ylab="% Disease Severity", 
     pch=16, 
     pty="s", 
     xlim=c(0,30), 
     ylim=c(0,30) 
) 
abline(severity.lm,lty=1) 
title(main="Graph of % Disease Severity vs Temperature") 

मैं किसी तरह का उपयोग करना चाहिए पाश और क्षेत्रों http://www.iiap.res.in/astrostat/School07/R/html/graphics/html/segments.html के लिए सीधा ऑफसेट करना है? क्या कोई और अधिक प्रभावी तरीका है? यदि संभव हो तो कृपया एक उदाहरण प्रदान करें।

उत्तर

16

आपको पहले लंबवत सेगमेंट के आधार के लिए निर्देशांकों को समझने की आवश्यकता है, फिर segments फ़ंक्शन को कॉल करें जो निर्देशांक के वैक्टर को इनपुट के रूप में ले सकता है (लूप की आवश्यकता नहीं है)।

perp.segment.coord <- function(x0, y0, lm.mod){ 
#finds endpoint for a perpendicular segment from the point (x0,y0) to the line 
# defined by lm.mod as y=a+b*x 
    a <- coef(lm.mod)[1] #intercept 
    b <- coef(lm.mod)[2] #slope 
    x1 <- (x0+b*y0-a*b)/(1+b^2) 
    y1 <- a + b*x1 
    list(x0=x0, y0=y0, x1=x1, y1=y1) 
} 

खंडों अब बस फोन:

ss <- perp.segment.coord(temperature, diseasesev, severity.lm) 
do.call(segments, ss) 
#which is the same as: 
segments(x0=ss$x0, x1=ss$x1, y0=ss$y0, y1=ss$y1) 

ध्यान दें कि परिणाम सीधा नहीं लग रही जब तक आप यह सुनिश्चित करें कि एक्स-इकाई और अपने भूखंड के y- इकाई ही स्पष्ट लंबाई (सममितीय तराजू)। आप स्क्वायर प्लॉट प्राप्त करने के लिए pty="s" का उपयोग कर ऐसा कर सकते हैं और उसी श्रेणी में xlim और ylim सेट कर सकते हैं।

+0

बिल्कुल सही, धन्यवाद। –