शुरू करेंमुफ़्त में शुरू करें

Regression के R-Squared को समझना

R-squared बताता है कि डेटा रिग्रेशन लाइन पर कितना अच्छा फिट होता है, इसलिए simple regression में R-squared दो वैरिएबल्स के बीच correlation से जुड़ा होता है. खास तौर पर, correlation का परिमाण R-squared के square root के बराबर होता है और correlation का चिह्न रिग्रेशन कोएफ़िशिएंट के चिह्न के समान होता है.

इस अभ्यास में, आप statsmodels नाम के स्टैटिस्टिकल पैकेज का उपयोग शुरू करेंगे, जो R और SAS तथा MATLAB जैसे सॉफ़्टवेयर पैकेजों में मिलने वाले अधिकांश स्टैटिस्टिकल मॉडलिंग और टेस्टिंग करता है.

आप दो series, x और y, लेंगे, उनका correlation निकालेंगे, और फिर statsmodels.api लाइब्रेरी में फंक्शन OLS(y,x) का उपयोग करके y को x पर रिग्रेस करेंगे (ध्यान दें कि dependent या right-hand side वैरिएबल y पहला argument है). अधिकांश linear regressions में एक constant term शामिल होता है जो intercept होता है (रिग्रेशन \(\small y_t=\alpha + \beta x_t + \epsilon_t\) में \(\small \alpha\)). OLS() फंक्शन का उपयोग करते हुए constant शामिल करने के लिए, आपको रिग्रेशन के right-hand side में 1's का एक कॉलम जोड़ना होगा.

मॉड्यूल statsmodels.api पहले से sm के रूप में इम्पोर्ट किया गया है.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में Time Series Analysis

पाठ्यक्रम देखें

अभ्यास निर्देश

  • .corr() मेथड का उपयोग करके x और y के बीच correlation निकालें.
  • एक रिग्रेशन चलाएँ:
    • पहले Series x को DataFrame dfx में बदलें.
    • sm.add_constant() का उपयोग करके एक constant जोड़ें और उसे dfx1 में असाइन करें.
    • sm.OLS().fit() का उपयोग करके y को dfx1 पर रिग्रेस करें.
  • रिग्रेशन के परिणाम प्रिंट करें और R-squared की तुलना correlation से करें.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

# Import the statsmodels module
import statsmodels.api as sm

# Compute correlation of x and y
correlation = ___
print("The correlation between x and y is %4.2f" %(correlation))

# Convert the Series x to a DataFrame and name the column x
dfx = pd.DataFrame(x, columns=['x'])

# Add a constant to the DataFrame dfx
dfx1 = sm.add_constant(___)

# Regress y on dfx1
result = sm.OLS(___, ___).fit()

# Print out the results and look at the relationship between R-squared and the correlation above
print(result.summary())
कोड संपादित करें और चलाएँ