Wednesday 15 November 2017

स्टेटा फॉरेक्स में एक मैट्रिक्स के आयाम


सूचना: आईडीआरई सांख्यिकी परामर्श समूह वेबसाइट को वर्डप्रेस सीएमएस में फरवरी में माइग्रेट कर देगा ताकि नई सामग्री के रख-रखाव और सृजन की सुविधा मिल सके। हमारे कुछ पुराने पृष्ठों को हटा दिया जाएगा या संग्रहीत किया जाएगा ताकि उन्हें अब बनाए रखा नहीं जाएगा हम रीडायरेक्ट बनाए रखने का प्रयास करेंगे ताकि पुरानी यूआरएल हम जितनी अच्छी तरह काम कर सकें उतना काम जारी रहेगा। डिजिटल रिसर्च और एजुकेशन फॉर डिजिटल रिसर्च एंड एजुकेशन में आपका स्वागत है एक उपहार देने के लिए Stat कंसल्टिंग ग्रुप द्वारा सहायता करें। स्टेटा एफएक्यू। मैं स्टैटा में एक कमांड चलाने के बाद संग्रहीत जानकारी का उपयोग कैसे कर सकता हूं? परिणाम परिणाम विंडो में दिखाए गए आउटपुट के अलावा, कई कमांड कमांड के बारे में जानकारी और स्मृति में इसके परिणाम संग्रहीत करता है इससे उपयोगकर्ता, साथ ही अन्य स्टेट आज्ञाओं को आसानी से इस जानकारी का उपयोग करने की अनुमति मिलती है। स्टेता इन लौटे हुए परिणामों को बताता है रिटायर्ड परिणाम बहुत उपयोगी हो सकते हैं जब आप स्टाटा में कुछ और करने के लिए स्टटा कमांड द्वारा उत्पादित जानकारी का उपयोग करना चाहते हैं उदाहरण के लिए, यदि आप चाहते हैं कि किसी केंद्र को एक चर, तो आप मतलब की गणना करने के लिए संक्षेप का उपयोग कर सकते हैं, फिर चर के मध्य में गणना की जाने वाली माध्य के मूल्य का उपयोग करें लौट आए परिणामों का उपयोग करना मतलब के मूल्य को फिर से टाइप करना या कटने और चिपकाने की आवश्यकता को समाप्त करेगा। परिणामस्वरूप दिए गए परिणामों का एक अन्य उदाहरण उपयोगी हो सकता है, यदि आप परिणाम वेरिएबल के अनुमानित मान तैयार करना चाहते हैं, जब भविष्यवक्ता चर विशिष्ट मानों के सेट पर होते हैं, फिर यहां आप गुणांक को पुनः टाइप कर सकते हैं या कट-पेस्ट का उपयोग कर सकते हैं, लेकिन परिणाम निकाल सकते हैं कार्य को बहुत आसान बनाओ परिणाम का काम कैसे लौटाता है यह समझने का सबसे अच्छा तरीका है कि सही में कूद और उनका उपयोग करना और उनका उपयोग करना शुरू करें। नीचे दिया गया कोड एक उदाहरण डेटासेट खोलता है और चर पढ़ने के लिए वर्णनात्मक आँकड़े उत्पन्न करने के लिए संक्षेप (संक्षिप्त योग) का उपयोग करता है यह अपेक्षित आउटपुट का उत्पादन करता है, लेकिन हमारे उद्देश्यों के लिए ज़्यादा महत्वपूर्ण बात, स्टाटा में मेमोरी में संग्रहीत सारांश सूची से परिणाम आता है लेकिन आप कैसे जानते हैं कि कौन सी जानकारी संग्रहीत की गई है प्रत्येक निर्देश द्वारा सहेजी गई सूचनाओं की एक सूची मदद फ़ाइल में और प्रिंट किए गए मैनुअल में शामिल है, इसलिए मैं वहां देख सकता हूं, लेकिन मैं केवल वापसी सूची टाइप कर सकता हूं। जो सभी लौटे हुए परिणामों को स्मृति में सूचीबद्ध करेगा ऊपर दिए गए परिणामों की एक सूची है, जैसा कि आप देख सकते हैं कि प्रत्येक परिणाम फॉर्म आर (।) का है जहां एल्हीस (quot; quot) एक छोटा लेबल है हम सूची में प्रत्येक आइटम को जानने के लिए संक्षेप कमांड के लिए मदद फ़ाइल देख सकते हैं, लेकिन उदाहरण के लिए, आर (मतलब) क्या परिणाम को सौंपा गया है, यह समझना अक्सर आसान होता है। आश्चर्य की बात में पढ़ने का मतलब है (आप इसे आउटपुट के खिलाफ देख सकते हैं), लेकिन दूसरों के रूप में स्पष्ट नहीं हैं, उदाहरण के लिए आर (sumw)। इन के लिए, आपको मैनुअल से परामर्श करना पड़ सकता है अगर आपको लगता है कि आप उनका उपयोग करना चाहेंगे। अधिकांश समय प्रक्रिया अपेक्षाकृत आसान होगी क्योंकि आपको पता चल जाएगा कि आप किस परिणाम का उपयोग करना चाहते हैं, आप सूची को देखने के बजाय सूची को देखे जाने की कोशिश कर रहे हैं और पता लगाने की कोशिश कर रहे हैं कि प्रत्येक नाम आइटम है जैसा कि आप कल्पना कर सकते हैं, अलग-अलग आज्ञाएं, और यहां तक ​​कि अलग-अलग विकल्पों के साथ एक ही कमांड, अलग-अलग परिणामों को स्टोर करते हैं। नीचे हम फिर से पढ़े गए चर को सारांशित करते हैं, लेकिन विस्तार विकल्प जोड़ें। फिर हम लौटे हुए परिणामों की सूची प्राप्त करने के लिए वापसी सूची का उपयोग करते हैं। जैसे ही विस्तार विकल्प उत्पादन में अतिरिक्त जानकारी जोड़ता है, वैसे ही लौटा परिणाम में संग्रहीत अतिरिक्त जानकारी का भी परिणाम है। नई सूची में उपर्युक्त राशि के ऊपर दिए गए सभी जानकारी शामिल है, साथ ही स्काइव कुर्टोसिस और कई संख्याएं शामिल हैं, जिनमें 1 (आर (पी 25)) और तीसरे (आर (पी 75)) क्वार्टिल्स और मध्य (आर (पी 50) )। अब जब हमें संक्षेप में दिए गए आदेशों से परिणाम प्राप्त होने का कुछ मतलब है, तो हम लौटा परिणाम का उपयोग कर सकते हैं। उपरोक्त उल्लिखित उदाहरणों में से किसी एक के माध्यम से, हम इसका मतलब होगा कि वे चर पढ़ें। यह मानते हुए कि हमने जो आखिरी आदेश दिया था, ऊपर दिए गए कमांड का सारांश दिया गया था, नीचे दिए गए कोड में एक नया चर उत्पन्न होता है, जिसमें पढ़ा जाने के माध्य केन्द्रित मान होते हैं। ध्यान दें कि इस आदेश में पढ़ने के मतलब के वास्तविक मूल्य का उपयोग करने के बजाय, हमने लौटा परिणाम (यानी आर (मतलब)) के नाम का इस्तेमाल किया है, स्ताता जानता है कि जब हम आर (मतलब) को देखते हैं, तो हम वास्तव में इसका अर्थ मानते हैं उस सिस्टम चर अगली पंक्ति पर हम नए चर क्रीड को सारांशित करते हैं। जबकि इसका मतलब शून्य के बिल्कुल बराबर नहीं है, यह शून्य की गोलाकार त्रुटि के भीतर है, इसलिए हम जानते हैं कि हमने सही तरीके से पढ़ा है चर पढ़ा है। उपर्युक्त कोड से पता चलता है, हम उसी तरह से लौटा परिणाम का उपयोग कर सकते हैं जिस तरह हम वास्तविक संख्या का उपयोग करेंगे। ऐसा इसलिए है क्योंकि स्टेटा आर (।) का प्रयोग वास्तविक प्लेसमेंट के लिए प्लेसहोल्डर के रूप में करता है। इसके एक अन्य उदाहरण के लिए, हम अपने मानक विचलन से पढ़ा जाने के विचलन की गणना करना चाहते हैं (इस तथ्य को अनदेखा करते हुए कि संक्षेप में आर (वार) में विचरण देता है) हम इसे कैलकुलेटर के रूप में प्रदर्शन कमांड का उपयोग कर उड़ने पर कर सकते हैं। नीचे दिए गए कोड की दूसरी पंक्ति यह है हम आउटपुट से मानक विचलन के मूल्य को काटें और चिपकाने के परिणाम भी देख सकते हैं, जो कि नीचे दिए गए तीसरे कमांड में किया जाता है। परिणाम मूल रूप से समान होते हैं, बहुत मामूली अंतर गोलाई त्रुटि है क्योंकि संग्रहित अनुमान आर (एसडी) में आउटपुट में प्रदर्शित मानक विचलन के मूल्य की तुलना में सटीकता के अधिक अंक होते हैं। लौटे हुए परिणाम, आर-क्लास और ई-क्लास के प्रकार अब जब आप लौटे हुए परिणामों के बारे में कुछ जानते हैं और आप उनका काम कैसे करते हैं तो उनके बारे में कुछ और जानकारी के लिए तैयार हैं। रिटर्न के परिणाम दो मुख्य प्रकार, आर-क्लास और ई-क्लास में आते हैं (एस-क्लास और सी-क्लास के परिणाम भी हैं, लेकिन हम उनसे यहां चर्चा नहीं करेंगे)। उदाहरण जो कि आकलन करते हैं, उदाहरण के लिए सभी प्रकार के रिग्रेसन, फ़ैक्टर विश्लेषण, और एनोवा ई-क्लास कमांड हैं अन्य कमांड, उदाहरण के लिए संक्षेप में, सहसंबंधित और अनुमानित आदेशों के लिए, r-class कमांड हैं आर-क्लास और ई-क्लास कमांड के बीच का अंतर महत्वपूर्ण है क्योंकि स्टेटा स्टोर्स विभिन्न क्लाइंट्स में ई-क्लास और आर-क्लास कमांड से परिणाम निकालते हैं। क्वाट यह आपके लिए उपयोगकर्ता के रूप में दो असर डालता है। सबसे पहले, आपको यह जानने की जरूरत है कि क्या परिणाम आर () या ई () में संग्रहीत किए गए हैं (साथ ही परिणाम का नाम) ताकि उनका उपयोग कर सके यदि आप सुनिश्चित नहीं हैं कि किस प्रकार से कमांड आप चलाते हैं, तो आप या तो मदद फ़ाइल में इसे देख सकते हैं, या एक जगह पर (यदि परिणाम को सूचीबद्ध करने के लिए उपयुक्त कमांड का उपयोग कर), तो उद्धरण को देखें, यदि परिणाम वहां संग्रहीत नहीं किए जाते हैं, तो संभवतः वे अन्य। आर-क्लास और ई-क्लास के आदेशों से परिणाम कैसे लौटाते हैं, इस अंतर के संभावित रूप से अधिक महत्वपूर्ण रैमिरीकरण यह है कि लौटा परिणाम केवल एक ही कक्षा के दूसरे आदेश को चलाने तक ही मेमोरी में रखे जाते हैं। यही है, पिछले आदेशों के परिणामों को एक ही कक्षा के बाद के आदेशों द्वारा प्रतिस्थापित किया जाता है। इसके विपरीत, किसी अन्य वर्ग के आदेश को चलाने से परिणामस्वरूप लौटा परिणाम प्रभावित नहीं होंगे। उदाहरण के लिए, यदि मैं एक प्रतिगमन चलाता हूं, और फिर एक दूसरे प्रतिगमन, पहले प्रतिगमन (ई () में संग्रहित किया जाता है) दूसरे प्रतिगमन (ई () में भी संग्रहीत होता है। हालांकि, यदि एक दूसरे प्रतिगमन के बजाय, मैं एक अनुमान के बाद आदेश चलाता हूं, तो प्रतिगमन के परिणाम ई () में बने रहेंगे, जबकि पोस्ट अनुमान आदेश से परिणाम r () में रखा जाएगा। हालांकि दोनों के बीच एक अंतर है, आर-क्लास और ई-क्लास कमांड के परिणामों का वास्तविक उपयोग बहुत समान है। शुरुआती के लिए, कमांड रिटर्न सूची में स्मृति में संग्रहीत r-class परिणामों की सूची के लिए, समानांतर हैं। ई-क्लास के परिणाम के लिए ऐसा करने के लिए आदेश ereturn सूची। इसके अलावा, नामकरण सम्मेलनों (r () बनाम ई ()) में अंतर के अलावा, परिणाम उसी तरह से पहुँचा जा सकता है। नीचे दिए गए उदाहरण में यह दर्शाया गया है, पहले हम महिला पर लिखने और पढ़ना छोड़ते हैं। और फिर लौटे परिणाम को देखने के लिए ईरेचरन सूची का उपयोग करें। रिग्रेस के लिए दिए गए परिणामों की सूची में हेडिंग स्केलर्स, मैक्रोज़, मैट्रिक्स और फ़ंक्शन के अंतर्गत सूचीबद्ध कई प्रकार के लौटे हुए परिणाम शामिल हैं। हम नीचे दिए गए परिणामों के प्रकारों पर चर्चा करेंगे, लेकिन अभी के लिए हम दिखाएंगे कि कैसे आप स्केलेयर परिणाम निकाल सकते हैं, उसी तरह से हमने लौटे हुए परिणामों का संक्षेप में इस्तेमाल किया था। उदाहरण के लिए, प्रतिगमन के बाद त्रुटियों के भिन्नता की गणना करने का एक तरीका स्वतंत्रता की कुल डिग्री (अर्थात एन -1) द्वारा अवशिष्ट अवयवों को विभाजित करना है। वर्गों का अवशिष्ट योग ई (आरएसएस) में संग्रहीत है और विश्लेषण के लिए एन ई (एन) में जमा है। नीचे हम कैलक्यूलेटर के रूप में प्रदर्शन कमांड का उपयोग करते हैं, त्रुटियों के भिन्नता की गणना करने के लिए लौटा परिणाम के साथ। परिणामों को कैसे वापस किया जाता है: स्केलर्स, स्ट्रिंग्स, मैट्रिक्स और फ़ंक्शंस जैसा कि ऊपर वर्णित है, आर-क्लास और ई-क्लास कमांड दोनों के लिए, स्केलेर्स, स्ट्रिंग्स, मैट्रिक्स और फ़ंक्शन सहित कई तरह के लौटे हुए परिणाम हैं। लौटा परिणाम की सूची में, प्रत्येक प्रकार अपने शीर्षक के अंतर्गत सूचीबद्ध होता है शीर्षक quotscalarsquot के तहत सूचीबद्ध परिणाम है कि, एक ही संख्यात्मक मूल्य। उनका उपयोग ऊपर चर्चा की है, इसलिए हम इस खंड में उनके बारे में अब और नहीं कह रहे हैं। Quotmacrosquot के अंतर्गत उल्लिखित परिणाम लौटाए जाते हैं जो आम तौर पर उस कमान के बारे में जानकारी देते हैं जो चल रहे थे। उदाहरण के लिए, ऊपर दिखाए गए प्रतिगमन के लिए दिए गए परिणामों में, ई (सीएमडीलाइन) में उपयोगकर्ता द्वारा जारी किए गए कमांड (बिना किसी संक्षिप्ताक्षर) शामिल हैं। ये आम तौर पर प्रोग्रामिंग स्टेटा में उपयोग किया जाता है। जैसा कि आप उम्मीद करेंगे, मैट्रिसेस के अनुसार कमेट्रीससॉट के तहत सूचीबद्ध परिणाम हैं। जबकि लौटाई गई सूची और erturn सूची द्वारा दिए गए परिणामों की सूची आपको लौटे हुए परिणामों के अधिकांश पर आधारित मान दिखाती है, लेकिन यह मैट्रिक्स के साथ व्यावहारिक नहीं है, बजाय मैट्रिक्स के आयाम सूचीबद्ध हैं। मैट्रिक्स की सामग्री देखने के लिए आपको उन्हें मैट्रिक्स कमांड का उपयोग करना चाहिए। हम इसे नीचे कमांड मैट्रिक्स लिस्ट ई (बी) का उपयोग करते हुए गुणांक (ई (बी)) के मैट्रिक्स के साथ करते हैं। (ध्यान दें कि एक मॉडल को फिट करने के बाद गुणांक और उनकी मानक त्रुटियों तक पहुंचने का एक और तरीका है, यह नीचे चर्चा की गई है।) अगर हम मैट्रिक्स पर मैट्रिक्स संचालन करना चाहते हैं या मैट्रिक्स के अलग-अलग तत्वों का उपयोग करना चाहते हैं, तो हम रिटर्न परिणाम के रूप में संग्रहीत मैट्रिक्स को एक सामान्य स्टेटा मैट्रिक्स में ले जाएँ। यह नीचे दिए वाक्यविन्यास के अंतिम पंक्ति में किया जाता है अंत में, शीर्षकों के मुताबिक परिणामों के मुताबिक रिक्त पदों में कार्य किया जाता है जो कि अन्य स्टेटा कार्यों के समान इस्तेमाल किया जा सकता है। Stata estimation कमांड्स द्वारा सबसे आम समारोह वापस आ गया है शायद ई (नमूना)। यह फ़ंक्शन पिछले विश्लेषण के आकलन में उपयोग किए गए नमूने को चिह्नित करता है, यह उपयोगी होता है क्योंकि डेटासेट में अक्सर अनुपलब्ध मान होते हैं जिसके परिणामस्वरूप दिए गए विश्लेषण में डेटासेट में सभी मामलों का उपयोग नहीं किया जाता है। यह मानते हुए कि पिछले आकलन आदेश चलाया गया था महिला पर लिखने का प्रतिगमन और ऊपर दिखाए गए पढ़ने के लिए, नीचे दिए गए कोड की पहली पंक्ति मॉडल में प्रयुक्त उन मामलों में पढ़ने का मतलब खोजने के लिए ई (नमूना) का उपयोग करती है। कोड की दूसरी पंक्ति का उपयोग ई (नमूना) को एक नया चर बनाते हैं, जो कि ध्वज नामक है, जो विश्लेषण के मामलों में 1 के बराबर है, और शून्य अन्यथा। (ध्यान दें कि उदाहरण के डेटासेट में कोई ग़लत डेटा नहीं है, सभी मामलों को विश्लेषण में शामिल किया गया है, और झंडा एक के बराबर स्थिर है।) गुणांक और उनकी मानक त्रुटियां ऊपर बताए गए अनुसार, एक मॉडल के बाद, गुणांक और उनके मानक त्रुटियों को मैट्रिक्स फॉर्म में ई () में जमा किया जाता है। ये मैट्रिक्स उपयोगकर्ता के गुणांकों तक पहुंच की अनुमति देते हैं, लेकिन स्टेटा आपको सिस्टम व्हेरिएबल्स बी और एसई में स्टोर करके इस जानकारी तक पहुंचने का एक आसान तरीका प्रदान करता है। प्रतिगमन के बाद एक प्रतिगमन गुणांक के मूल्य तक पहुंचने के लिए, सभी को एक प्रकार की वर्णाम कहा जाता है, जहां वर्नाम को भविष्यवाचक चर का नाम दिया गया है, जिसका गुणांक आप जांचना चाहते हैं। मानक त्रुटि तक पहुंचने के लिए, आप बस सेवर्नम टाइप कर सकते हैं। स्थिरांक के गुणांक और मानक त्रुटि तक पहुंचने के लिए हम क्रमशः bcons और secons का उपयोग करते हैं। नीचे हम उसी प्रतिगमन मॉडल को चलाते हैं जो हम उपर्युक्त (उत्पादन को छोड़कर), महिला का उपयोग करते हुए और लिखने की भविष्यवाणी पढ़ते हैं। एक बार जब हमने मॉडल का अनुमान लगाया है, तो हम डिस्प्ले कमांड का उपयोग यह दिखाने के लिए करते हैं कि बी में मान हमारे प्रतिगमन गुणांक के बराबर हैं। अंत में, हम लिखने के अनुमानित मूल्य की गणना करते हैं जब एक महिला (महिला 1) छात्र का पठन स्कोर 52 होता है। इस वेब साइट की सामग्री को किसी विशेष वेब साइट, किताब या सॉफ़्टवेयर उत्पाद का समर्थन नहीं किया जाना चाहिए कैलिफोर्निया विश्वविद्यालय। माता माता की एक परिचय एक मैट्रिक्स भाषा है जो कई मायनों में आर, मैटलैब या गौस के समान है। इसमें कुछ अनोखी और पेचीदा विशेषताएं हैं एक यह है कि यह एक संकलित भाषा है जो व्याख्या की जाती है, जो प्रदर्शन को बेहतर बनाता है इसे स्टैटापाप (सभी एसएससीसी लिनक्स सर्वर और कोंडोर पर उपलब्ध) में समानांतर किया गया है जो नाटकीय ढंग से प्रदर्शन को बेहतर बनाता है। दूसरी ओर माता बिल्कुल नया है और एसएससीसी पर अभी तक नहीं निकले हैं, इसलिए हम पेशकश करने के लिए किसी वास्तविक दुनिया की तुलना नहीं करते हैं। माता स्ताता के प्रतिस्थापन नहीं है, न ही इसका एकमात्र एकमात्र सांख्यिकीय पैकेज है। यह एक ऐसा उपकरण है जिसे स्टाता (या एसएएस या एसपीएसएस) के पूरक के रूप में सबसे अच्छा इस्तेमाल किया जाता है, उन चीजों को करने के लिए कि स्ताता अपने आप पर अच्छा नहीं करता है विशेष रूप से, माता एकल डेटा सेट के संदर्भ में काम नहीं करता है, इसे अतिरिक्त लचीलापन प्रदान करता है। लेकिन जब तक आप स्टेटा या किसी अन्य सांख्यिकीय पैकेज से पहले से ही परिचित नहीं हैं, तब तक आप माता को सीखने की कोशिश नहीं करनी चाहिए। माता एक अपेक्षाकृत क्लोलो लेवलकॉट भाषा है स्ताता (या एसएएस या एसपीएसएस) में ज्यादा वक्त बिल्ट-इन प्रोग्राम्स का उपयोग करके बिताया जाता है, स्टेटा को आप जो चाहें करने के लिए विकल्प के सही संयोजन का पता लगाते हैं। माता में आप सीधे नियंत्रण लेते हैं, माता को बताते हुए कि आप कदम-दर-चरण क्या करना चाहते हैं। (माता अनुकूलक, जिसे हम लम्बे समय पर चर्चा करेंगे, एक उल्लेखनीय अपवाद है।) इसका मतलब है कि साधारण से बातें करना Stata की तुलना में माता में अधिक कठिन है, लेकिन माता की कम बाधाएं हैं। यह आलेख मुख्यतः स्टेटा, एसएएस या एसपीएसएस सिंटैक्स का उपयोग करने वाले लोगों के लिए लिखा गया है, लेकिन कोई अन्य प्रोग्रामिंग भाषा नहीं है। इस प्रकार मैट्रिक्स को जोड़कर उपयोगी चीजें सीखने पर बहुत जोर दिया जाएगा, और कई उदाहरण ऐसे अनुभव देने के साथ-साथ एक विशेष अवधारणा को स्पष्ट करने के लिए डिज़ाइन किए गए हैं। Matlab और GAUSS दिग्गजों पा सकते हैं कि वे इन वर्गों को शिकस्त कर सकते हैं, जो उनके लिए नया है पर ध्यान केंद्रित कर सकते हैं। सी प्रोग्रामर यह पाएंगे कि माता जब भी कर सकते हैं, तो सी का अनुकरण करता है, इसलिए वे संभवतः लूपों जैसे मानक प्रोग्रामिंग निर्माणों के वर्गों को हटा सकते हैं। लेकिन कोई बात नहीं कि आपकी पृष्ठभूमि क्या है, अगर आप इस लेख को कंप्यूटर पर पढ़ते हैं, तो स्टाटा चल रहे हैं, और वास्तव में उदाहरणों में टाइप करें। माता पत्थरों के भीतर चलती है, इसलिए माता का उपयोग करने के लिए आपको पता होना चाहिए कि स्टाटा प्रोग्राम कैसे चलाना है, जिसे एक फाइल कहा जाता है। यदि आपने कभी स्ताता का इस्तेमाल नहीं किया है, तो कृपया Stata के लिए एक परिचय में फाइलों पर अनुभाग पढ़ें। इंटरएक्टिव स्टेटा (यानी इसे शुरू करें और कमांड टाइप करें) यह जानने का एक शानदार तरीका है और यह कि आप इस आलेख में उदाहरण कैसे दिखाएंगे। लेकिन वास्तविक काम के लिए आप सभी फाइलें लिखना चाहते हैं। इस आलेख से जुड़े कई उदाहरण फ़ाइलें हैं पाठ में उनके लिंक्स के रूप में उपयोग किया जाता है हालांकि, यदि आप एक एसएससीसी लिनक्स सर्वर का उपयोग कर रहे हैं तो आप उन सभी समय के आगे नकल कर सकते हैं। ऐसा करने के लिए लिनक्स प्रॉम्प्ट पर निम्नलिखित टाइप करें: mkdir mataclass cd mataclass cp usrglobalwebssccpubsfiles4-26। आपके द्वारा पढ़े जाने वाले अधिकांश आदेशों को भी मैटैक्लास में शामिल किया गया है। दो प्रमुख उदाहरण ex1.do और ex2.do में पाए जाते हैं माता मूल बातें माता को शुरू करने के लिए, पहले स्टाटा शुरू करें और फिर कमांड विंडो में टाइप करें। स्टेटा फिर "मेटा मोड" पर स्विच कर देगा.तुम्हें यह भूलना आसान है कि आप किस मोड में हैं, लेकिन अगर आप समीक्षा विंडो के नीचे सावधानी से देखते हैं, जहां सामान्य तौर पर आप एक अवधि देखते हैं तो आपको अब एक बृहदान्त्र दिखाई देगा। बेशक, जब आप पहली बार माता शुरू करते हैं, तो एक बड़ी क्षैतिज रेखा होती है जो माता कहती है, लेकिन यह लंबे समय तक दिखाई नहीं देती। उस बार आपको एक अन्य उपयोगी कमान की भी याद दिलाता है: माता मोड से बाहर निकलने के लिए, टाइप करें जबकि स्टेटा आज्ञाओं के आसपास आयोजित किया जाता है, माता को कोटेशन के आसपास का आयोजन किया जाता है। उदाहरण के लिए, आप बस टाइप कर सकते हैं और माता वापस लौट आएंगे परिणाम को एक चर में संग्रहीत करना बस के रूप में आसान: ध्यान दें कि इस समय कोई आउटपुट नहीं था। यदि आप एक्स का मान देखना चाहते हैं, तो बस टाइप करें: और आपको 4 वापस मिलेंगे। मैट्रिक्स ऑपरेटर्स हमारे पहले उदाहरणों में, प्लस हस्ताक्षर एक ऑपरेटर के रूप में काम करते हैं: माता ने इसे क्वाटलके को प्लस साइन और प्लस चिह्न के बाद की बात से पहले कहा था और उनसे सम्मिलित किया था। सॉट मेट मैट्रिक्स ऑपरेटर को भी परिभाषित करता है जो मैट्रिक्स मैनिपुलेशन लागू करते हैं। कॉलम और पंक्ति में शामिल करें अल्पविराम को quotcolumn joinquot ऑपरेटर के रूप में परिभाषित किया गया है, या अल्पविराम से पहले और बाद में उन चीजों को क्वोटेट किया गया है और उन्हें एक दूसरे के बगल में डाल दिया जाता है। इसका मतलब है कि स्ताटा द्वारा एक पंक्ति और दो स्तंभों के साथ एक मैट्रिक्स के रूप में व्याख्या की जाती है। वास्तव में, इसे टाइप करें और आउटपुट होंगे: बैकस्लैश (जो नहीं, जो विभाजन है) कोट्टो जॉक्क्वाट ऑपरेटर है, या बैकस्लैश से पहले चीज को दबाएं और उसके बाद चीज़ के शीर्ष पर स्टैक करें.इस प्रकार यह एक मैट्रिक्स है दो पंक्तियां और एक स्तंभ: चीजों को संचालित करने के लिए स्केलर तक सीमित नहीं हैं, इसलिए आप मैट्रिक्स का निर्माण कर सकते हैं। त्रुटि संदेशों में सीमाएं हैं: ltistmtgt: 3200 conformability त्रुटि r (3200) आप एक मैट्रिक्स नहीं बना सकते हैं जिनकी पहली पंक्ति में दो स्तंभ हैं, लेकिन जिनकी दूसरी और तीसरी पंक्तियों में एक कॉलम है स्ताता यह एक अनुरूपता त्रुटि के रूप में कहता है, एक वर्ग जिसमें मैट्रिक्स को गुणा करने की कोशिश करने जैसी चीजें भी शामिल होती हैं, जहां पहले मैट्रिक्स के कॉलम की संख्या दूसरे की पंक्तियों की संख्या से भिन्न होती है। यह एक उद्धरण त्रुटि भी है जब आप माता को एक बयान देते हैं, तो यह पहली बात है कि कथन को बाइट कोड में संकलित करना है। उस बिंदु पर माता विशेष रूप से छेड़छाड़ की जाने वाली विशिष्ट मात्राओं को नहीं देख रही है। यह देखता है कि यह कुछ अन्य के आगे क्वाटपुट है, फिर दो अन्य चीजों के शीर्ष पर स्टैक। यह समझ में आता है, इसलिए बयान सफलतापूर्वक संकलित करता है जब कोड चलता है तो मैटस नोटिस है कि चीजों को स्टैक करने के लिए अलग-अलग कॉलम हैं। कम्पाइल समय त्रुटि तब उत्पन्न होती है जब कथन मतलब नहीं होता है, चाहे आप उसमें कितनी मात्रा में प्लग करें। उदाहरण के लिए, अमान्य अभिव्यक्ति r (3000) संकलन समय त्रुटियाँ हमेशा त्रुटि कोड 3000 हैं, और शामिल नहीं ltistmtgt। यह देखते हुए कि कोई त्रुटि एक संकलन समय त्रुटि या एक रनटाइम त्रुटि समस्या को खोजने में आपकी सहायता कर सकती है। अगर यह रनटाइम था, तो आप जानते हैं कि माता को कम से कम आपका कोड समझा गया है (हालांकि इसमें कोई गारंटी नहीं है कि इसका मतलब यह समझना चाहिए कि आप इसका मतलब समझते हैं) और यह उस विशिष्ट मात्रा के बारे में कुछ था जो उस समस्या के कारण काम कर रहा था। यदि इसकी संकलन समय, कोड भी माता को समझ में नहीं आता है पैरेंटेशस जैसा कि आप एक साथ मैट्रिक्स डाल रहे हैं, चीजों को स्पष्ट करने में मदद करने के लिए कोष्ठकों को शामिल करने के लिए आपका स्वागत है। यह आपके लाभ के लिए है: माता के लिए सभी समान हैं, लेकिन अगर उनमें से एक आपके सभी तरीकों से स्पष्ट दिखता है तो अपने सभी कार्यक्रमों में उस शैली का उपयोग करें। (मैं तीसरे का प्रशंसक बनूंगा।) रेंज ऑपरेटर्स मैट्रिक्स बनाने के लिए आसान उपकरण का एक और सेट श्रेणी ऑपरेटर हैं I । ऑपरेटर बाएं से नंबर की संख्या से दाईं ओर की संख्या से शुरू की एक श्रृंखला बनाता है और उन्हें एक पंक्ति वेक्टर में बना देता है । ऑपरेटर समान करता है लेकिन उन्हें एक कॉलम वेक्टर में डाल देता है यह पूर्णांक तक सीमित नहीं है, हालांकि: ध्यान दें कि दाईं ओर की संख्या को परिणामी श्रृंखला का हिस्सा होने की गारंटी नहीं है। मैट्रिक्स स्टेटमेंट के परिणाम निश्चित रूप से अन्य वक्तव्य की तरह चर में सहेजे जा सकते हैं। उदाहरण के लिए: नोट करें कि z की परिभाषा कैसे बहुत पहले दिखती है जैसे कि हम पहले की कोशिश की थी जो रनटाइम त्रुटि दे दी थी। यह अब चलता है क्योंकि एक्स और वाई दोनों के दो कॉलम हैं। यही वजह है कि यह एक रनटाइम त्रुटि थी और एक संकलन समय त्रुटि नहीं थी: सही निविष्टियां दी गई हैं, बयान कार्य कर सकता है अंकगणित ऑपरेटर मानक अंकगणित ऑपरेटरों को पता है कि उनके एक या दोनों तर्क एक मैट्रिक्स है, और तदनुसार कार्य करते हैं। इसमें अतिरिक्त और घटाव, स्केलर बार मैट्रिक्स और पूर्ण मैट्रिक्स गुणा शामिल है। हालाँकि, यहां तकलकनक्वाट ऑपरेटर भी हैं जो तत्व द्वारा तत्व का काम करते हैं। इसके अलावा और घटाव के लिए यह कोई फर्क नहीं पड़ता, क्योंकि वैसे तत्व द्वारा वे तत्व हैं। लेकिन यह गुणन के साथ बहुत अंतर बनाता है निम्नलिखित पर विचार करें: लॉजिकल ऑपरेटर्स लॉजिकल ऑपरेटर्स जैसे मैट्रिक्स और कॉलन संस्करणों में भी (जो कि असाइन ऑपरेटर के बराबर है, असाइनमेन्ट के विरोध में) से भी कम, और बराबर के रूप में परिभाषित किया गया है। हालांकि, माता में एक बूलीय चर प्रकार नहीं है। इस प्रकार (स्ताटा के रूप में) तार्किक ऑपरेटर गलत के लिए सही और शून्य के लिए एक लौटे हैं। यदि आप मैट्रिक्स की तुलना में मानक, अधिक से कम या बराबर का उपयोग करते हुए दो मैट्रिक्स की तुलना करते हैं, तो माता एक देता है अगर और केवल अगर स्थिति दो मैट्रिक्स के सभी तत्वों के लिए सही है। अन्यथा यह शून्य देता है ये ऑपरेटर केवल एक ही आकार के मैट्रिक्स पर काम कर सकते हैं। उनके बृहदान्त्र रूप में, नतीजा एक मैट्रिक्स है जिसमें सभी तत्व-दर-तत्व तुलना शामिल हैं। बृहदान्त्र रूप से तुलना करने के लिए मैट्रिक्स के आकार के बारे में अधिक लचीलेपन की अनुमति देता है। अगर एक तर्क एक स्केलर, पंक्ति वेक्टर या कॉलम वेक्टर है, तो इसे दोहराया जाएगा ताकि अन्य मैट्रिक्स के मिलान के लिए आवश्यक हो। Transpose ऑपरेटर सही एकल उद्धरण है ()। यह सिर्फ एक ऑब्जेक्ट पर काम करता है, एक इसके बायीं ओर, और इसके ट्रांज़ोज़्ज़ रिटर्न करता है: हालांकि, कोई व्यस्त ऑपरेटर नहीं है, हालांकि उलटा कार्यों द्वारा किया जाता है मैट्रिक्स के किसी विशेष तत्व का संदर्भ देने के लिए सबस्क्रिप्ट, मैट्रिक्स नाम के बाद पंक्ति और स्तंभ संख्या को चौकोर ब्रैकेट में डाल दिया। ऊपर दिए गए मैट्रिक्स एक्स को देखते हुए, x1,1 है 1. x1,2 है 2. आदि। सबकुछ एक बराबर चिह्न के दोनों तरफ इस्तेमाल किया जा सकता है: यदि मैट्रिक्स एक पंक्ति या स्तंभ वेक्टर है, तो आप केवल एक सबस्क्रिप्ट का उपयोग कर सकते हैं। पंक्ति या स्तंभ संख्या के लिए quotingquot quoted सभी पंक्तियों या स्तंभों के लिए लिया जाता है आप या तो अनुपस्थित होने के लिए मानक अवधि का उपयोग कर सकते हैं या बस नंबर बाहर छोड़ सकते हैं। इस प्रकार आप किसी पंक्ति या कॉलम वेक्टर (और स्ताता की देखभाल करते हैं जो आप उपयोग करते हैं) के साथ संख्या को भी बदल सकते हैं। हालांकि, इस बार कोष्ठकों की आवश्यकता होती है, इसलिए माता जानता है कि पंक्तियों की सूची समाप्त होती है और कॉलम शुरू होते हैं। आप पंक्तियों या स्तंभों को दोहरा सकते हैं, ऑर्डर को मिला सकते हैं या यहां तक ​​कि मूल से अधिक मैट्रिक्स भी बना सकते हैं। पूर्व-परिभाषित वैक्टर भी ठीक ही हैं: नोट करें कि इस तरह से वेक्टर सबस्क्रिप्ट्स का उपयोग करना क्रमिक मैट्रिक्स द्वारा गुणा करना अधिक तेज होगा। सबस्क्रिप्टिंग रेंज माटा का प्रयोग उन सबस्क्रिप्टों से है, जो कि हमने अभी तक उपयोग किए हैं। हम चाहते थे कि प्रत्येक तत्व को स्पष्ट रूप से सूचीबद्ध किया गया था (अच्छी तरह से, अर्थात् अर्थहीन लापता को छोड़कर)। लेकिन आप अक्सर पंक्तियों और स्तंभों की एक श्रेणी निर्दिष्ट करना चाहते हैं। छोटे मैट्रिक्स के साथ श्रेणियों के लिए बहुत ज़्यादा ज़रूरत नहीं होती है, इसलिए बड़ी मैट्रिक्स बनाकर शुरू करें: आगे पढ़ने से पहले यह पता लगाने की कोशिश करने के लिए कुछ समय निकालें। पहली पंक्ति में रेंज ऑपरेटर का उपयोग करके पंक्ति वेक्टर के रूप में एक्स 1 को परिभाषित करता है। दूसरा एक एक्स 2 कॉलम वेक्टर को परिभाषित करता है, फिर से रेंज ऑपरेटर का उपयोग करता है, लेकिन परिणाम दस से गुणा करता है। तीसरा तो एक्स को परिभाषित करता है क्योंकि x1 की दस प्रतियां एक-दूसरे के शीर्ष पर खड़ी होती हैं और एक-दूसरे के बगल में स्थित एक्स 2 की दस प्रतियां परिणाम दस दस मैट्रिक्स होता है जो 11 से 110 तक जाता है, जिससे यह बताना आसान होगा कि अगले कुछ उदाहरणों में कौन से पंक्तियों और स्तंभों को निकाल दिया गया है। (हां, यह कार्य और अन्य लूपों का उपयोग करने के लिए आसान तरीके हैं, लेकिन हमने उन्हें अभी तक कवर नहीं किया है।) एक्स से पंक्तियों और स्तंभों की एक सीमा को निकालने का एक तरीका है सूची सबस्क्रिप्ट्स में श्रेणी ऑपरेटरों का उपयोग करना। इस प्रकार पंक्तियाँ 3-7 और कॉलम 4-8 प्राप्त करने के लिए आप ऐसा कर सकते हैं: यह बिल्कुल टाइप करने के बराबर है: हालांकि, सच श्रेणी सबस्क्रिप्ट, हालांकि, अलग-अलग हैं। एक बात के लिए, वे वर्ग कोष्ठक और पाइप चरित्र (शिफ्ट बैकस्लैश) में निहित हैं। दो तत्व हैं, बैकस्लैश द्वारा अलग किए गए हैं: वांछित सीमा के ऊपरी बाएं कोने के पंक्ति और स्तंभ और वांछित सीमा के निचले दाएं कोने में। इस प्रकार, चार से सात वस्तुओं की जगह की जगह के रूप में इसके बारे में सोचने के बराबर और पंक्ति चार, स्तंभ तीन और पंक्ति सात, स्तंभ आठ, सम्मिलित। यदि ऊपरी बाएं कोने को निर्दिष्ट करने में उपयोग किया जाता है, तो इसका मतलब है कि पहली पंक्ति या प्रथम स्तंभ। यदि निचले दाहिने कोने को निर्दिष्ट करने में उपयोग किया जाता है, तो इसका अर्थ है कि अंतिम पंक्ति या अंतिम स्तंभ। इस प्रकार बस एक्स के सभी होते हैं वहां भी कोई नियम नहीं है जो ऊपरी बायां और दाएं कोने को एक ही पंक्ति या स्तंभ पर नहीं बता सकता इसका मतलब है कि तीसरे स्तंभ से अंत तक तीन को उद्धृत करें। स्कॉट स्टेटा से माता को अपना डेटा प्राप्त करना अधिकांश समय आप हाथों से मैट्रिक्स बनाना नहीं चाहते हैं। इसके बजाय आप पहले से ही Stata में मौजूद डेटा युक्त मैट्रिक्स बनाना चाहते हैं। आप भी अपने माता के परिणाम लेना चाहते हैं और उन्हें अपने स्ताटा डेटा सेट में कॉपी कर सकते हैं। आपको जवाब देने के लिए सबसे पहले प्रश्न यह है कि क्या आप अपने डेटा की एक नई प्रतिलिपि बनाना चाहते हैं, या आप सीधे मेटा डेटा के साथ माता काम करना चाहते हैं या नहीं माता मैट्रिक्स को परिभाषित कर सकते हैं जो कि वास्तव में आपके स्टैटा डेटा के उद्धरण चिह्न हैं एक दृश्य के बारे में सोचिए, सिर्फ अपना स्टेटा डेटा देकर एक नाम माता का उपयोग कर सकते हैं। दृश्य आपके डेटा बहुत बड़ी हैं, भले ही स्मृति का एक तुच्छ राशि का उपयोग करें। यदि आप किसी दृश्य मैट्रिक्स की टेबल बदलते हैं, तो स्टाटा डेटा भी बदल दिया जाता है। यदि आप इसके बजाय डेटा की प्रतिलिपि बनाते हैं तो आप परिवर्तन कर सकते हैं और मूल अक्षुण्ण बनाए रख सकते हैं, हालांकि आप दो बार जितनी मेमोरी का उपयोग करेंगे। अपने डेटा की प्रतियां बनाने के लिए आप कार्य के स्टैटाटा परिवार का उपयोग करेंगे। सबसे आसान और सबसे तेज है stdata इसका उपयोग आपके डेटा सेट से एक नंबर प्राप्त करने के लिए किया जाता है। इसमें दो तर्क हैं: एक अवलोकन संख्या और एक चर संख्या पहले से ही मैट्रिक्स के रूप में आपके डेटा सेट के बारे में सोचें, और चर संख्या केवल आपके इच्छित चर की कॉलम संख्या है। यह कार्रवाई में देखने के लिए, अंत टाइप करके माता का पहला अंत। ऑटोमोबाइल उदाहरण डेटा सेट लोड करें, एक सूची अवलोकन करें, और माता में वापस जाएं: 1 मीटर में अंत सिस्यूज़ ऑटो एल अब पहली कार की कीमत टाइप करके टाइप करें: परिणाम 4099 होगा, जो आपके द्वारा अभी सूचीबद्ध किए गए मेल से मेल खाता है। यह सुनिश्चित करने के लिए कुछ अन्य चर की कोशिश करें कि आपके पास यह विचार आया है। ध्यान दें कि यदि आप पहले कॉलम प्राप्त करने का प्रयास करते हैं तो आपको ग़लत मूल्य मिलेगा। Thats क्योंकि stdata केवल संख्यात्मक डेटा के लिए है तारों के लिए, stsdata का उपयोग करें जो बहुत ज्यादा उसी तरीके से काम करता है। माता अपने आप के लिए स्तंभ संख्याओं को देख सकते हैं stvarindex फ़ंक्शन का उपयोग कर। यह आपके द्वारा इच्छित चर का नाम लेता है और सूचकांक देता है। हालांकि, यह प्रक्रिया कुछ धीमा कर देती है ध्यान दें कि आप किसी अन्य फ़ंक्शन के तर्क के रूप में एक फ़ंक्शन के आउटपुट का उपयोग करने के लिए स्वागत करते हैं: यदि आप एक से अधिक मान चाहते हैं, तो stdata का उपयोग करें stdata केवल stdata (हालांकि उस मामले में stdata तेज है) की तरह इस्तेमाल किया जा सकता है। हालांकि, यह स्वीकार किए जाने वाले तर्कों के बारे में stdata अधिक लचीला है। पंक्ति संख्या गायब हो सकती है, इस स्थिति में सभी टिप्पणियां वापस कर दी जाती हैं। यह एक कॉलम वेक्टर भी हो सकता है, जिस स्थिति में केवल निर्दिष्ट अवलोकन वापस कर दिए जाते हैं। यह दो कॉलम के साथ मैट्रिक्स भी हो सकता है। प्रत्येक पंक्ति फिर अवलोकन की एक श्रेणी का प्रतिनिधित्व करती है। इस मामले में लापता एक अंतिम अवलोकन के रूप में व्याख्या की जाती है। कॉलम संख्या भी गायब हो सकती है (सभी चर) या वांछित चर को सूचीबद्ध करने वाला एक वेक्टर यह उनकी संख्याओं के बजाय वेरिएबल्स के नामों को भी स्वीकार कर सकता है, हालांकि यह धीमा हो जाएगा। यह पंक्तियों के लिए आपके जैसे रेंज को निर्दिष्ट करने की अनुमति नहीं देता है Stdata की तरह stdata तार को संभाल नहीं सकते लेकिन एक समकक्ष stsdata है निम्न प्रयास करें: stdata (।) Stdata ((1,3), 2) stdata (1, (2,4)) stdata (1, (quotpricequot, quotrep78quot)) stsdata (।, 1) स्वाभाविक रूप से इन सभी परिणाम हो सकता है भविष्य के उपयोग के लिए मैट्रिक्स में संग्रहीत stdata तीसरे तर्क भी ले सकता है: एक चयन चर यह या तो सूचक या एक चर का नाम हो सकता है, और यदि यह निर्दिष्ट किया गया है तो केवल वे टिप्पणियां हैं जहां वे चर शून्य के बराबर नहीं हैं। 0 को चुनिंदा चर के रूप में चुनना एक विशेष अर्थ है: उस मामले में टिप्पणियों को बाहर रखा जाएगा यदि वे निर्दिष्ट किसी भी वैरिएबल के लिए गुम वैल्यू हैं स्टॉप उन्हें नकल करने के बजाय आपके डेटा पर दृश्य बनाता है, लेकिन आप stdata जैसी सटीक विधियों का उपयोग करके पंक्तियों और चर का चयन कर सकते हैं। हालांकि, स्ट्रेव आपको परिणामों को एक अलग तरीके से देता है। Stdata के साथ मैट्रिक्स जिसे आप चाहते थे वह था जो stdata फ़ंक्शन वापस लौटा था। फिर आप परिणामों को स्टोर कर सकते हैं या बस उन्हें स्क्रीन पर प्रदर्शित कर सकते हैं। स्टाव के साथ फ़ंक्शन ही कुछ भी नहीं देता है इसके बजाय आपको एक मैट्रिक्स में पास करना होगा जो आपके द्वारा चुने हुए दृश्य से बदल दिया जाएगा। एक पकड़ यह है कि मैट्रिक्स को इसे पारित करने से पहले ही मौजूद होना चाहिए। इससे कोई फर्क नहीं पड़ता कि यह कितना बड़ा है - वास्तव में इसे पूरी तरह से बदल दिया जाएगा - लेकिन अगर आप एक मैट्रिक्स को स्ट्रेव करने का प्रयास करते हैं जो किसी भी तरह परिभाषित नहीं हुआ है एक रनटाइम त्रुटि एक आसान तरीका यह है कि बस मैट्रिक्स को सेट करना है जिसे आप इसे पास करने से पहले शून्य में उपयोग करना चाहते हैं। आप यह भी अपने फ़ंक्शन कॉल के अंदर कर सकते हैं: x और n अब एक नियमित मैट्रिक्स की तरह दिखते हैं, लेकिन ध्यान रखें कि वे वास्तव में देखे गए हैं । यदि आपका मकसद, मेमोरी को सहेजने के लिए आपका उद्देश्य है, तो आपको इसकी प्रतिलिपि बनाने के लिए सावधान रहना होगा। एक नया दृश्य नहीं बनाते यह एक नया मैट्रिक्स बनाता है जिसमें प्रतिलिपि के x के मान शामिल होते हैं। अधिक आसानी से, एक्स की जेथ कॉलम की प्रतिलिपि बनाता है। अगर आपको एक्स के बाहर एक विशेष स्तंभ की आवश्यकता है, तो आप सिर्फ एक नया दृश्य बना सकते हैं: अलग-अलग दृश्यों की संख्या पर कोई सीमा नहीं है, जो आप उसी डेटा के लिए सेट अप कर सकते हैं। माटा से स्ताटा तक अपना डेटा प्राप्त करना यदि आप सभी करना चाहते हैं, तो मौजूदा चर के मूल्यों को बदलना होगा, इच्छाओं को देखने और परिवर्तन करना होगा। लेकिन अगर आप नए वेरिएबल जोड़ना चाहते हैं तो आपको कुछ और फ़ंक्शन की आवश्यकता होगी। स्टडवर्जर एक नया चर जोड़ता है इसमें दो तर्क हैं: चर प्रकार और चर नाम यह चर के सूचक को देता है, भले ही भविष्य में उपयोग वैकल्पिक है। चलो एक नया चर बनाते हैं: weightmpg (इतना, गैलन प्रति पौंड मील, एक इकाई केवल एक इंजीनियर प्यार कर सकता है)। ध्यान दें कि mpg नामक मैट्रिक्स अब एमपीजी चर के सभी पंक्तियों का एक दृश्य है। स्टैव्यू (वेट 0 रेटाइटक्वॉट) pmgweight: एमपीजी स्टडवर्र (कोट्लॉन्गक्वाट, कोटपीजीगॉट) स्टस्टोर (।,,,,,, पीएमजी) स्टेटा टूल्स से परिचित हुए, टाइप एंड से परिचित इस नए चर का पता लगाने के लिए। बेशक यह जनरल के लिए एक असाधारण अनाड़ी प्रतिस्थापन है, जो बहुत लंबा pmgmpgweight है यह एक सामान्य सिद्धांत की ओर जाता है: इसके अच्छे के लिए माता का उपयोग करें, और इसके अच्छे के लिए स्ताटा ऐसे अन्य कार्यों की एक विस्तृत विविधता है जो आप माता और स्ताता के बीच संवाद करने के लिए उपयोग कर सकते हैं, जिसमें स्थानीय मैक्रोज़, ई () और आर () वैक्टर और अवलोकन शामिल करना और सेटिंग करना शामिल है, और बहुत कुछ। विवरण के लिए मैनुअल देखें बचत डेटा लोड करना और लोड करना अक्सर एक माता कार्यक्रम का लक्ष्य अपने स्टेटा डेटा को लेने, नतीजे प्राप्त करना, और फिर या तो इसे प्रदर्शित करना या इसे अपने स्टैटा डेटा में वापस करना है हालांकि, यदि आप की आवश्यकता होती है तो आप अपने माता मैट्रिस को स्वयं बचा सकते हैं। मैट्रिक्स को सहेजने के लिए, टाइप करें मेटा मैट्सव फाइलनाम मैट्रिक्सलिस्ट फ़ाइल नाम को उस फ़ाइल के नाम से बदलें जिसे आप बनाना चाहते हैं। स्टाटा फ़ाइल नाम के स्वचालित रूप से.mmat जोड़ देगा। एक या एक से अधिक मैट्रिक्स के साथ मैट्रिक्स सूची को बदलें जिसे आप वर्तमान में स्मृति में सभी मैट्रिक्स को सहेजने के लिए सहेजना चाहते हैं (रिक्त स्थान के साथ अलग करना) या तारांकन () के साथ आपके द्वारा पहले सहेजे गए मैट्रिक्स को लोड करने के लिए, टाइप करें माटा मैट्यूज फ़ाइलनाम यह फ़ाइल नाम में संग्रहीत सभी मैट्रिक्स को लोड करेगा। दोनों कमांड प्रतिस्थापित विकल्प को स्वीकार करेंगे। मैटसेव के लिए यह माता को डिस्क पर मौजूदा फ़ाइल को अधिलेखित करने की अनुमति देता है। मैट्यूज के लिए इससे माता को स्मृति में मौजूदा मैट्रिक्स को फ़ाइल में मैट्रिक्स के नामों के समान नाम से ओवरराइट करने की अनुमति मिलती है। ये कमांड इंटरैक्टिव उपयोग के लिए अभिप्रेत हैं और फ़ंक्शंस में उपयोग नहीं किए जा सकते। यदि फ़ंक्शन के भीतर आपको मैट्रिक्स को बचाने की आवश्यकता है, तो फ़ॉपन की जांच करें fputmatrix। fgetmatrix। और fclose Hierarchical Data Hierarchical data has always been a bit awkward to work with in Stata, or any other statistical program that uses a single data matrix. A typical example would be individuals living in households: should each household be one observation, or each individual Either way there are inefficiencies. In Mata you can have it both ways: one matrix of individuals and one matrix of households. The key is linking them together, but subscripts make that easy. As an example, take a look at the hh Stata data set. which is in Stata format. It consists of six individuals living in three households. hh is the household ID, hhType is the type of household, and hhInc is the household income. age and female are individual variables. This data is in the long form, with one observation for each individual, which means that the household variables must be duplicated. end use hh, replace l Now enter mata and load the hh Mata data set . mata mata matuse hh hh ind It contains two matrices, ind and hh. hh contains the household level variables ( hhType and hhInc ). ind contains the individual variables ( age and female ) plus the household ID. Note however, that hh does not contain an ID: the row number is an implicit ID. For example, if you wanted to know what type of household person number two lives in, youd use: ind2,3 is the household ID of the second person, or the row in the hh matrix. Column one of hh is the household type. Of course a regression model may need a single matrix just like Stata does. You can easily recreate the matrix as Stata views it with: Here ind.,3 is a column vector listing which row we want from hh for each row of ind. The resulting rows from hh are then placed next to ind to create x . Matrix Functions Mata has a very large number of matrix functions built into it. This section will hit some of the most useful. Creating Matrices The I function can take one or two arguments. If it is given one argument, it will return an identity matrix of size equal to the argument it was given. If it is given two arguments, it will return a matrix with that number of rows and columns which is full of zeroes except for ones along the principal diagonal. The J function creates a matrix of constants. It takes three arguments: the number of rows of the matrix to be created, the number of columns, and what to put in the matrix. The last argument can be of any type. The e function creates unit vectors: row vectors with a one in one column and zeroes everywhere else. It takes two arguments: the location of the one, and the size of the row vector. Thus you could create an identity matrix by combining e s, though the I function would of course be much easier. The uniform function returns a matrix filled with random numbers distributed uniform(0,1). The size is specified in the same way as with the J function. If youre putting together a matrix which will be symmetric, the makesymmetric function can take care of half the work for you. You put together the lower triangle, and makesymmetric will copy it to the upper triangle (replacing what was there before). It takes one argument, a matrix, and returns the symmetric version. x(1,2)(3,4) ymakesymmetric(x) y Theres a second version, makesymmetric which returns nothing but changes the input matrix instead: The sort function returns a sorted matrix. It takes two arguments: the matrix to sort, and the column(s) to sort by. Note that sort is not a quotstablequot sort. If there are any ties, they will be resolved randomly. Repeat sort(x,1) enough times and you should see some cases when the original second row becomes the first row and others where it stays the second row. If the second argument is a row vector, the matrix will be sorted by the first column listed, then ties resolved by the second column, etc. sort does not change the matrix it is given--it returns a copy. If youd prefer to sort the original matrix without making a copy, use sort . jumble and jumble are the opposite of sort and sort. They take just one argument, a matrix, and put its rows in a random order. Sizes of Matrices rows. cols. and length all take one argument, a matrix. They return the number of rows, the number of columns, and the total number of elements (rowscolumns) in that matrix respectively. rows(x) cols(x) length(x) These can be very useful in for loops where you want to loop over your entire matrix but dont know ahead of time what size it will be. Of course matrices can have missing values just like ordinary Stata data sets. If you need to know the number of missing values in the whole matrix, a row, or a column, use missing. rowmissing or colmissing respectively. missing returns a scalar, rowmissing returns a column vector (one value for each row in the matrix), and colmissing returns a row vector (one value for each column in the matrix). y(1,2,.)(4. 6)(7,8,.) missing(y) rowmissing (y) colmissing(y) To get the number of non-missing values, using nonmissing. rownonmissing. or colnonmissing . Descriptive Statistics Mata has a set of functions for calculating descriptive statistics, though by design its not as rich as Statas. In their simplest and most commonly used forms, all these functions take a single matrix as their argument. Sums may be stretching the definition of quotdescriptive statisticquot but they are very useful. There are three main sum functions: sum. rowsum. and colsum. sum adds up the whole matrix, returning a single number. rowsum adds up each row, returning a column vector. colsum adds up each column, returning a row vector. sum(x) rowsum(x) colsum(x) max and min simply return the largest or smallest element of the matrix. rowmax and rowmin return a column vector containing the largest or smallest element of each row. Likewise, colmax and colmin return a row vector. You can get the largest and smallest elements with the minmax functions, including minmax. rowminmax. and colminmax. They return both the min and the max in either two columns or, in the case of colminmax. two rows. Sometimes youre more interested in where the min or max is than what it is. If so, check out minindex and maxindex. The mean function returns a row vector containing the column means of the matrix. If you need row means, youll need to construct them yourself: You can also get the variance matrix of your matrix with variance, and the correlation matrix with correlation. Matrix Characteristics Mata has functions for finding the most common matrix characteristics. diagonal returns the principal diagonal of a matrix as a row vector. trace returns the sum of the diagonal elements. det returns the determinant (with some round-off error). rank returns the rank of the matrix. As a general rule, using rank to check that a matrix is full rank because a subsequent calculation requires it is redundant--better to let the subsequent calculation fail and handle the failure. Also, when determining the rank of a matrix on a computer a certain tolerance is required for round-off error. Different functions can use different tolerances, so they may disagree with the rank function. Solvers, Inverters and Decomposition Matrix solvers are functions designed to solve the equation AXB for X. Since inverting a matrix A can be done by solving the equation AXI, inverters and solvers are closely related. In addition, the solvers and inverters generally work by doing some sort of decomposition, and the decomposition methods can also be accessed directly. Thus there are several families of three related functions. Which family youll choose depends on the properties of your matrix. Well focus on Cholesky methods, but the others work in a very similar way. The Cholesky Decomposition decomposes a symmetric, positive definite matrix into a lower triangular matrix times its transpose. The results can be used to solve matrix equations and find inverses much more quickly than more general methods. To get an example matrix we can work with, we can take advantage of the fact that if x is of full rank, xx is symmetric and positive definite--and random matrices are all but certain to be full rank. So: The cholesky function takes one argument, a matrix, and returns a lower triangular matrix which is its Cholesky Decomposition. To verify that it works, note that is the same as a. To illustrate solving a matrix equation, we need a right-hand side. The cholsolve function takes two arguments, both matrices. It them sets up and solves the equation AXB where A is the first matrix and B is the second, returning X. To verify that it worked, note that The cholinv function takes one argument, a matrix, and returns its inverse: Again, to verify it worked try The result should be an identity matrix. Youll notice that round-off error makes some of the zeroes not quite zeroes, but its as close as you can get using a computer. If your matrix is square but not symmetric, LU Decomposition can do similar things (though its slower than Cholesky). The lud function is equivalent to cholesky. but more complex because it has to give back three results. Thus the matrices to store the results have to be passed in, similar to stview . LU Decomposition breaks a matrix into a lower triangular matrix L, an upper triangular matrix U, and a permutation matrix P. But Mata, instead of using a matrix P, gives a column vector p which can be used with subscripting to do the same thing. Take look at L. U. and p. To verify that it worked, see that is a. Note how were using subscripts to pull rows from a result, not an existing matrix, and yet it works just fine. lusolve and luinv are much simpler--in fact the usage is identical to cholsolv and cholinv. Try Similar functions exist for QR decomposition ( qrd. qrsolve. qrinv ). Singular value decomposition has svd and svsolve. but the related inverter is pinv (which returns the Moore-Penrose pseudoinverse). invsym. for inverting symmetric matrices, has no related decomposition or solver functions. It is, however, what Stata suggests for linear regression. Example: Linear Regression Youre now prepared to do the most common matrix manipulation of all, at least at the SSCC. Since we have the automobile data set loaded, lets regress price on mpg, rep78, foreign and weight to see which characteristics American consumers were willing to pay for in 1978. One complication is that rep78 is missing for some observations, so youll need to drop them from your regression. Of course you could just drop them from the data set entirely, but lets assume that you want to keep them for later use. The first step is to mark the observations you can actually use. Exit Mata by typing end. then create a new variable touse which is one if all the variables are non-missing and zero otherwise. gen touse(price. amp mpg. amp rep78. amp weight. amp foreign.) Of course in reality the only variable that is ever missing is rep78. but well pretend we didnt know that ahead of time. If you wanted to run a regression on a subpopulation (say, just the foreign cars) you could add a condition to mark the subsample too. Now go back into Mata by typing mata. Next make x a view onto all the independant variables and y a view onto the dependant variable. touse is the selection variable for both views: observations will only be included if it is equal to one. stview(x0. (quotmpgquot, quotrep78quot, quotweightquot, quotforeignquot), quottousequot) stview(y0. (quotpricequot), quottousequot) To include a constant term in the regression you need to add a column of ones to x. Use the J function to create it, then add it to x with the comma (column join) operator. Now you can find the betas: Of course they dont mean anything without standard errors. Start by finding the residuals: Then the variance-covariance matrix is The standard errors for each beta can be extracted using the diagonal function, along with sqrt. which takes the (element by element) square root. The t-statistic is the beta divided by the standard error, but this is an element by element division so you need to use the colon operator. To find the p-values requires the ttail function, which takes as arguments the degrees of freedom and the t-statistic and returns the probability. It is a single-tailed test however, so you need to multiply by two. To put your results together in a readable form, try: Now exit Mata again, and check your results against reg price mpg rep78 weight foreign They should be identical. In regular Stata, if is almost always used to define which observations a command should act on. Its the equivalent to SQLs where clause. In Mata, if is only used to control program flow. (Though Stata can use if in this way as well--see the appropriate section of Programming in Stata .) The most basic form of if is simply if ( condition ) statement If condition is true, then statement is executed. Otherwise it is skipped entirely. Note that the condition must be in parentheses. If you want to do more than one thing if the condition is true, use the following syntax: if ( condition ) statements You can also use else to specify things that should happen if the condition is not true. Thus you can create fairly elaborate structures: if ( condition1 ) statements1 else if ( condition2 ) statements2 else statements3 Keep in mind that each condition has a single result, either true or false. For example, If x is a matrix you cant write if (xgt5) and then list things you want done for those elements of x which are greater than 5. You can write if (x1,1gt5) followed by if (x1,2gt5) etc. but of course the easy way to do that is using a loop. Mata has while. do - while. and for loops available (plus goto for easier conversion of FORTRAN code, but we dont want to endorse spaghetti logic). while looks very similar to if: while ( condition ) statements The difference is that the statements will be executed over and over as long as the condition is true. Thus your first concern should be to make sure that at some point the condition will become false, or the loop will run forever. Note that x is shorthand for xx1. Even though we said xlt5, the final value of x is six. Thats because the loop only decides to end when x becomes six. You can also put a while loop with a single statement in a single line. This loop is perfectly legal, but it didnt do anything. The reason is that the condition started out false, so the loop never executed at all. Sometimes you need to make sure that a loop runs at least once, and thats a job for do-while. A do-while loop starts with do and ends with the while condition. The statements are always executed at least once. If the condition is true, the loop starts over again from the top. Note that x is increased to seven even though it started out greater than five. One typical use for do-while loops is to do something until it converges: do complex mathematical stuff while (abs(newvalue-oldvalue)gttolerance) But unless youre absolutely certain your process will actually converge, youd better put in an escape clause that tells it to stop after a while whether it converges or not: do complex mathematical stuff while (abs(newValue-oldValue)gttolerance amp iterationltmaxIterations) Recall our first while loop: This is such a common structure that programmers wanted a quicker way to construct it. The answer was for loops. The for loop equivalent to this while loop is Note the three components: an initialization step, a rule for when the loop should end, and some sort of progression towards that ending. Strictly speaking you can skip the initialization and the progression--just leave the semi-colons as placeholders. All this really means though is that youre promising to take care of those steps yourself. In particular x must already be defined and you need to make sure the loop will in fact end. By far the most common use of for loops is to loop over the elements of a matrix. Writing Your Own Functions Mata allows you to write your own functions--in fact many of the standard functions weve used are written in Mata. As youve seen, calling a function is a matter of typing the function name and then giving a list of arguments in parentheses. Defining a function follows the same structure: first the word function to tell Mata what youre doing, then a name, and then a list of arguments in parentheses. The body of the function follows in curly brackets. If you want your function to return a result, one of the statements in the body needs to be return. followed by the thing to return in parentheses. function myfunction(x, y) statements return(z) As an example, consider the following (not terribly useful) function. function doubleAndSum(x) xx2 return(sum(x)) This function takes a matrix, multiplies it by the scalar two, and returns the sum of all the elements in the matrix. To test it, try the following: mI(3) doubleAndSum(m) m Note that you passed in a matrix called m even though the function calls it x. Thats fine: the doubleAndSum function refers to whatever it is given as x. In fact the input doesnt even have to have a name. In the first case, the argument passed in is the result of the I function. In the second, it is a row vector defined on the spot. On the other hand, in these cases we cant see how the input matrix was doubled as they arent stored anywhere. Function arguments in Mata are quotpassed by reference. quot If a function changes one of the arguments it is given, that change persists even after the function is completed. Example: Loops and Functions Our next example comes from basic physics, but the real point is using loops and functions to get useful results. The classic equation for the motion of a body under uniform acceleration is simply Depending on the level of mathematics used in your last physics course, you may or may not have worked with that as a vector equation, where x, x0,v0 and a are vectors with one number for each dimension. The vector component makes this a good problem for Mata. Lets track and plot the movement of a falling object for ten seconds. Begin by defining a function x which takes x0,v0,a and t as arguments and returns the position of the body at time t. This is a good point to pause and test what weve done so far before moving on. Try: This represents a body just moving at 10 meterssecond (not that Mata cares about the units) in the positive x direction, so it will be easy to tell if your function works properly or not. should give 100,0,0. But that doesnt guarantee our acceleration term is right, so try This should give 0,0,50. Now well set up the actual values we want to examine: This is an object starting 500 meters up, moving to the right at 100 meters per second, and falling under normal earth gravity (about 9.8 meters per second squared). Note how we suddenly switched from three dimensions to two. Your function wont care in the slightest since theyre all still matrices, but its a lot easier to plot on a two dimensional graph. Now you need somewhere to put the results. Mata doesnt do graphs, so youll have to use Stata to plot them. But first we have to think about what the results will be. Your function provides a snapshot of the object at any given time, so well take a thousand snapshots spread across the ten second span. Each snapshot will have two numbers, an x coordinate and a y coordinate, so those will be your variables (though to Mata theyre just column one and column two of the x row vector). Get out of Mata, then use standard Stata commands to set up the observations and variables youll need. Now get back into Mata, and set up a view of these variables. Call it r (as in results): Now youre ready to loop over your 1000 snapshots and get the results of the x function for each one: Note how the time for each snapshot, i100, spreads them evenly across 10 seconds as i goes from 1 to 1000. Finally exit Mata again, and create the graph. end scatter y x Its the classic parabola of projectile motion as you no doubt expected, but of course the real point was to practice using functions and loops to generate and work with data. As soon as we defined the x() function, Mata compiled it into quotobject code. quot This is not quite the same as machine-language code that can be run all by itself. But it is something Mata can understand and run very quickly. If you were planning to use this function in the future you could save the object code. Then future programs wouldnt need to spend time compiling it. If this is something youre interested in, see the mlib and mata mosave commands. This example is contained in ex1.do. A pointer is a variable which contains the memory address of another variable or matrix. Thus it quotpointsquot to that other variable. In principle a pointer is just a number, but youll never work with the number directly. Instead, youll use two operators, amp and . Computer scientists call these the quotreferencequot and quotdereferencequot operators, but I like to think of them as quotthe address ofquot and quotthe thing at. quot Consider the following: First we define a matrix x so we have something to work with. Then p is defined as quotthe address of x. quot Thus p or quotthe thing at p quot is just another name for x. You can use subscripts with p just like you would with x. but note how you have to put p in parenthesis first. (p)2,2 means quotfind the thing at the address contained in p and get the value at row two, column two. quot p2,2 would mean quotget the value at row two, column two of p. and then find the thing at that addressquot which wont work because p is a scalar. Note that the matrix you are pointing to doesnt need to have a name. For example: makes p point to a 5x5 matrix of zeroes (since thats what J returns) even though theres no direct name for that matrix. Its only accessible as p. One use for pointers is to construct data structures Mata doesnt handle automatically. For example, you can construct a three dimensional matrix by making a two dimensional matrix of pointers, each of which points to a column vector. The first step is to define the matrix that will contain the pointers. Matrices cant switch between containing numbers and containing pointers, so you need to make sure that the matrix is defined as containing pointers. On the other hand, we dont have any actual pointers to store yet. Thus well set the initial values of the matrix to NULL. a special pointer that doesnt point to anything. Now loop over all the elements of x, and make each one a pointer to a new (and unique) column vector. To work with an element i, j,k of your three dimensional matrix youd use the following: Yes, the parentheses are essential. Its rather awkward though, so if you were going to work with such matrices a lot consider defining the following functions: function put(val, x,i, j,k) Usage: value to put, matrix to put it in, i, j, k to put it at. ((xi, j))k,1val function get(x, i,j, k) Usage: matrix to get from, i, j, k of value to get. return(((xi, j))k,1) Then you can do things like: The fact that you cant mix pointers and regular data within a matrix does limit your flexibility. You cant, for example, have a matrix of individual data including pointers to other data relevant to the individual. You can, however, have two parallel matrices, one containing numbers and the other containing pointers. If row i represents the same individual in both matrices, you can pull information from either or both as needed. Its also possible to create a pointer to a function. Given your existing function doubleAndSum() try: Note how Mata distinguishes between x() the function and x the variable by the parentheses, even though youre not passing in any arguments when you define a pointer. The main reason youd want to create a pointer to a function is so that you can use that pointer as the argument of a function. For example, Matas optimizer functions have you pass in a pointer to the function which is to be optimized. The Mata Optimizer Many statistical operations involve maximizing or minimizing some quantity--maximum likelihood estimation being the obvious example. Mata includes an optimizer for these operations. Matas optimizer is actually a family of functions which define and then solve an optimization problem. Evaluator functions The first function must be written by you: the function to be maximized or minimized. This function can do whatever you want, it may be very complex or very simple, but it must accept a certain set of arguments and return the proper result in order for the optimizer to use it. You can name the arguments whatever you like, but there must be the right number and each must have the proper meaning. Optimization problems are described using a variety of notations, but if we consider the problem of maximizing yf(x), the Mata version of f() must take the following arguments: a number indicating whether the function is supposed to calculate (0) just f(x), (1) yf(x) and f(x) or (2) f(x), f(x) and f(x) x (the thing that changes as the optimizer looks for the max) y (where f(x) will be stored) A variable to store f(x) if calculated A variable to store f(x) if calculated (often H since it is the hessian in multivariate problems) If you can find an analytic solution for f(x) and f(x) and code them in, Matas optimizer will be much faster and more accurate. This is often impractical though, and Mata is perfectly willing to find numeric approximations to the derivatives it needs. Note, however, that even if your function never calculates derivatives it must still accept variables where they can be stored. So lets try a very simple function: y-x4. If youre willing to let Mata find all the derivatives for you, you can code this as the following: Note that todo (the number telling the function whether to calculate derivatives or not), g. and H are completely unused, but must still be accepted. Note also that we could change all the names. We could call x Fred and y George as long as Georgef(Fred). If were not quite so lazy we can also code the derivatives for this function quite easily: In this case, both x and y were scalars, but youre not limited to scalars. Consider y-x14 - x24 The two xs are stored in a row vector x, so x1 is x1 and x2 is x2. Making sure the optimizer sends in the right size of x is one of the steps in setting it up. In many cases the quantity youll want to maximize will be the sum of a column. For example, in maximum likelihood you will probably create a column where each row gives an observations contribution to the likelihood function. Matas optimizer will do this automatically with the proper settings, so the previous function could be recast as: Note how the function had to define y as a column vector of the proper size--otherwise it is a scalar. In statistical applications the quantity to be maximized will depend not just on parameters that can vary (x in our problems thus far) but on data that do not vary. Matas optimizer can be set up to pass up to nine additional arguments to your evaluator function, which can contain the data. They go after the first two arguments (and before the final three). This calls for a change of variable names. Consider maximizing sf(b). Well now use x for the data matrix. Then the function definition would be: function f(todo, b, x, s, g, H) x can contain both the independent and dependant variables, but if its easier to work with a separate matrix y, then the definition becomes: function f(todo, b, x, y, s, g, H) Well do an example using this kind of evaluator shortly. Setting Up and Running an Optimization Problem Once youve got your evaluator function defined, youre ready to set up the optimization problem. The first step is to call the optimizeinit function. optimizeinit takes no arguments and returns a variable containing a description of your optimization problem. Youll never look at this description, but you will pass it in to all the other optimization functions. Next tell it where to find the evaluator: The first argument is the problem description, and the second is a pointer to the evaluator function you already defined. Now give it a starting value for x--remember the f() function is in the form yf(x). Of course the correct answer is zero, but we want it to do some work Now youre ready to actually run the optimizer: This returns the value of x which maximizes f(x). You may want to store this in a variable: If we want to use the g(x) function instead, theres one additional step. Recall that in g(x) we coded the first and second derivatives ourselves so Mata doesnt have to approximate them. Mata refers to this as a quotd2quot evaluator. A quotd1quot evaluator codes just the first derivative, and a quotd0quot evaluator codes no derivatives at all (the f(x) function is a d0 evaluator). Mata will assume functions are d0 unless we say otherwise, so you need to add: Note that the optimizer doesnt care what order all the initialization functions are called in, as long as theyre before the actual optimize(). What about h(x1, x2) Its identical to f(x), except that we need to set an initial value for both variables: In doing so we also tell Mata that future xs must have two columns. Finally, i(x1,x2), where the function to be maximized is the column sum of y, is what Mata calls a quotv0quot evaluator. The quotvquot is for vector, and the quot0quot again means that we didnt code any derivatives. Thus we need To see the complete code to run the optimizer with each evaluator, see the the last parts of mataclass. do. There is one setup function we havent needed to call but you should know: changes the problem from maximizing the function (the default) to minimizing it. Example: Ranking Teams As a final, extended example, consider a problem familiar to any sports fan: determining how good a team is based on its wonloss record. Well assume that a team can be characterized by a single quotstrengthquot variable s. If team i plays team j, well assume that the probability of team i winning is given by exp(si-sj)(1exp(si-sj)), better known to Stata as invlogit(si-sj). Then, given a record of games played and who won, we can find a set of values for s that maximizes the probability of the given outcome. Well do a Monte Carlo study by first creating data which fits our assumptions, and then seeing how well the method works. Creating the Data The first step is to create the data. This is an exercise in matrix manipulation, so if you want to focus on the optimization part of the problem feel free to skip ahead. On the other hand, most readers will benefit from some practice in this area. First create a row vector str containing the real strengths of each team. For simplicity, use the uniform function, giving strengths distributed uniform(0,1). For our example well make 50 teams: Note that the column number within the str vector acts as a sort of team ID. Next we need a way to keep track of who played who. Well create a two-column matrix, where each row is a game and the two columns will contain the IDs of the two teams who played in that game. For brevity well call the team in column one the quothomequot team and the team in column two the quotvisitingquot team. Well assume that each team plays 20 games, 10 as the home team and 10 as the visiting team. Begin by creating a column vector teams containing the 50 teams using the range operator: Now create a column vector season which is just ten copies of teams stacked on top of each other: seasonteams for(i1ilt10i) seasonseasonteams This represents the home teams. Next assign the visiting teams by taking the same vector, putting it in a random order, and column joining it to the original: The only trouble is, its entirely possible for a team to be randomly assigned to play itself. This wouldnt really bother our estimator, but it does offend any claims that this represents the real world. More importantly, fixing it is good practice. Create a column vector same with the same number of rows as season which contains a 1 if the home and visiting teams are the same and a 0 if they are not: If a game has the a team playing itself, we will swap the visiting team with the visiting team of a randomly chosen game. Since its possible we might get the same team yet again, well keep checking and swapping until there are no more games between the same team. Heres the code: while (max(same)1) for(i1 iltrows(season) i) if (samei) swaptrunc(uniform(1,1)rows(season))1 tempseasonswap,2 seasonswap,2seasoni,2 seasoni,2temp same(season.,1:season.,2) Note how max(same) will be zero if there are no longer any games where the home and visiting teams are the same, so thats how we know when were done. We then loop over the rows, stopping to change those where samei is one (or true). In those cases, we pick a random row and swap visiting teams with it, using a temp variable to store its visiting teams ID as we do. We then recalculate same based on the new version of season before the while condition is reevaluated. Now we need to decide who won each game. Well create a column vector winner, which will contain a 1 if the home team won and a zero if the visiting team won. winnerJ(rows(season),1,.) for(i1 iltrows(season) i) winneriuniform(1,1):ltinvlogit(strseasoni,1-strseasoni,2) Note how indexing is used to pull up the strength ( str ) of the appropriate team--well be doing that a lot. The Maximum Likelihood Estimator Now were ready to construct the (log) likelihood function to be maximized. Well start with a version thats easy to understand, and try to make it efficient later. function llf(todo, strhat, season, winner, llf, g,H) llfJ(rows(season),1,.) for(i1iltrows(season)i) if (winneri) llfilog(invlogit(strhatseasoni,1-strhatseasoni,2)) else llfilog(invlogit(strhatseasoni,2-strhatseasoni,1)) This will be a v0 evaluator which takes season and winner as additional arguments. The estimated strengths are stored in strhat, and the column of log likelihoods (which will be summed automatically by virtue of being v0) is stored in llf . We have two possible outcomes, and the formula for finding the log likelihood is different in each outcome. For now well handle the two possibilities with an ifelse structure, but there are more efficient ways. Now to set up the optimization problem: soptimizeinit() optimizeinitevaluator(s, ampllf()) optimizeinitevaluatortype(s, quotv0quot) strhat0J(1,rows(teams),.5) optimizeinitparams(s, strhat0) optimizeinitargument(s,1,season) optimizeinitargument(s,2,winner) strhat1optimize(s) Most of these youve seen before. Note that strhat0 is the vector of starting values for our estimate of str. Since the actual strengths are distributed uniform(0,1) well start by setting them all to 0.5. What is new is optimizeinitarguments. This is where you tell the optimizer to pass in season and winner to your evaluator. As you see optimizeinitarguments takes three arguments: the optimization problem, the number of the argument youre setting, and what to pass in. Run the code. It will take a while but it should work. Efficiency So how can we make it faster It would be nice if we didnt have to figure out which formula to use for the likelihood. So lets rearrange the data a bit: instead of column one being the quothomequot team and column two the quotvisitingquot team, terms which have no real meaning in our model, lets make column one the winner and column two the loser. Create a new matrix season2 with the new arrangement: season2J(rows(season),2,.) for(i1iltrows(season)i) if (winneri) season2i,.seasoni,. else season2i,.seasoni,(2,1) Of course this loop takes time to run, but it only runs once. Its the evaluator that must be run over and over, so taking a bit more time to set things up so that the evaluator runs faster is well worth it. One general principle when it comes to writing fast Mata code is that matrix operations are faster than loops you write out. Theres no matrix operation that would allow you to look in one matrix for the estimated strength of a team identified in another matrix, but you can take calculating the log and invlogit functions out of the loop. Heres a second and more efficient version of the evaluator function: function llf2(todo, strhat, season, llf, g,H) xJ(rows(season),1,.) for(i1iltrows(season)i) xistrhatseasoni,1-strhatseasoni,2 llflog(invlogit(x)) Note how it doesnt need to have the winner matrix passed in anymore--it expects a version of season ( season2 ) that conveys that information by which team is in column one. Heres the setup needed to run this version: soptimizeinit() optimizeinitevaluator(s, ampllf2()) optimizeinitevaluatortype(s, quotv0quot) strhat0J(1,rows(teams),.5) optimizeinitparams(s, strhat0) optimizeinitargument(s,1,season2) strhat2optimize(s) Youll see that this runs in about half the time of the original. Most of the gain comes from moving the calculation of log and invlogit out of the loop. For those looking for the absolute best performance, consider turning on matastrict ( mata set matastrict on ). Matastrict requires that you declare the names and types of all variables before using them rather than letting Mata choose. Mata has more variable types than most languages, and they can be confusing. On the other hand, declaring your variables can help you avoid errors. More importantly, Matas compiler can use the additional information to create slightly more efficient object code. If you are interested in using matastrict, see the manuals and especially the section on declarations. Constraints One characteristic of this model is that only the difference between teams is identified. You could add 100 or -1,000,000 to all the strengths and the probability of each outcome would remain the same. Thus two different runs on the same data could give very different numbers and both be right. However, if we constrain just one strength to be a given number, then all the strengths are identified. Matas optimizer accepts constraints on the parameters in the form of two matrices C and c. The parameters p are then constrained such that Cpc . Lets constrain the strength of team one to be zero. To implement this, the C matrix needs to be a row vector with a column for each team. It will have a one in the first column and a zero in all other columns, which makes it a unit vector. c will be simply the scalar zero. You then pass in this constraint using the optimizeinitconstraints function. It takes two arguments: the problem s. as usual, and then a matrix which is the row join of C and c. Im not sure why it doesnt just take them as two separate arguments, but its easy to join them. Since youve made no other changes to the problem, you can simply run it again by calling optimize. This version will actually run significantly faster. As an exercise, consider constructing some sort of metric for how well your estimator does. (One easy one would be how often it correctly identifies the best team.) Then vary the number of games per season and see how many it takes to get reasonable accuracy. However, dont do this if you want to continue to take the ranking systems of actual sports seriously. This example (with additional commands for timing each method) is found in ex2.do. Learning More This article has just scratched the surface of whats possible in Mata. Theres obviously much more to learn, and even more to be looked up when you need it. As usual, Stata Corp. has included most of the Mata documentation in the online help. There is one trick though: to get help for Mata you need to type help mata topic rather than just help topic. This is especially important for functions and such that exist in both Mata and Stata. For example, compare the results of the following: help abs help mata abs A couple useful starting places: help mata help mata functions The Mata manuals are available in the CDE Library and the 4218 lab. There are two books, but they should be thought of as two volumes of the same manual. You are also welcome to ask the Help Desk for assistance. Mata is new to us as well, but well try to figure things out together. Last Revised: 10242007

No comments:

Post a Comment