Sunday 12 November 2017

Quadratwurzel Von Ein Matrix In Stata Forex


Willkommen beim Institut für Digitale Forschung und Bildung Stata FAQ Wie kann ich auf gespeicherte Informationen zugreifen, nachdem ich einen Befehl in Stata ausgeführt habe (zurückgegebene Ergebnisse) Zusätzlich zu der Ausgabe, die im Ergebnisfenster angezeigt wird, speichern viele Statas-Befehle Informationen über den Befehl Und seine Ergebnisse in Erinnerung. Dies ermöglicht es dem Benutzer, ebenso wie andere Stata-Befehle, diese Informationen leicht zu nutzen. Stata nennt diese zurückgegebenen Ergebnisse. Zurückgegebene Ergebnisse können sehr nützlich sein, wenn Sie Informationen verwenden möchten, die von einem Stata-Befehl erzeugt werden, um etwas anderes in Stata zu tun. Wenn Sie z. B. eine Variable zentrieren möchten, können Sie mit summarize den Mittelwert berechnen und dann den Mittelwert verwenden, um die Variable zusammenzufassen, um die Variable zu zentrieren. Die Verwendung der zurückgegebenen Ergebnisse eliminiert die Notwendigkeit, den Wert des Mittelwerts erneut einzugeben oder zu schneiden und einzufügen. Ein weiteres Beispiel dafür, wie zurückgegebene Ergebnisse nützlich sein können, ist, wenn Sie die vorhergesagten Werte der Ergebnisvariablen generieren möchten, wenn sich die Vorhersagevariablen auf einem bestimmten Wertsatz befinden. Wiederum können Sie die Koeffizienten erneut eingeben oder ausschneiden und einfügen Machen die Aufgabe viel einfacher. Der beste Weg, um ein Gefühl, wie die Ergebnisse der Arbeit erhalten, ist zu springen rechts in und starten Sie suchen und mit ihnen. Der folgende Code öffnet ein Beispiel-Dataset und verwendet summarize (abgekürzte Summe), um deskriptive Statistiken für die Variable lesen zu erzeugen. Dies erzeugt die erwartete Ausgabe, aber noch wichtiger für unsere Zwecke, hat Stata nun Ergebnisse aus dem zusammengefassten Befehl im Speicher gespeichert. Aber wie Sie wissen, welche Informationen gespeichert worden sind Eine Auflistung der Informationen, die von jedem Befehl gespeichert werden, ist in der Hilfedatei und in der gedruckten Anleitung enthalten, so konnte ich dort aussehen, aber ich kann auch einfach geben Sie return Liste. Die alle zurückgegebenen Ergebnisse im Speicher auflisten wird. Oben ist eine Liste der zurückgegebenen Ergebnisse, wie Sie sehen können, jedes Ergebnis ist von der Form r (.), Wo die Ellipsen (ist ein kurzes Label). Wir konnten die Hilfedatei für den Zusammenfassungsbefehl sehen, um herauszufinden, was jeder Punkt auf der Liste ist, aber es ist oft einfach, herauszufinden, welchen Wert zu welchem ​​Ergebnis, z. B. r (Mittelwert) zugewiesen wird. Nicht überraschend enthält den Mittelwert der gelesen (können Sie dies gegen die Ausgabe), aber andere sind nicht so offensichtlich, zum Beispiel r (sumw). Für diese, müssen Sie das Handbuch zu konsultieren, wenn Sie denken, Sie könnten sie verwenden möchten. Die meisten der Zeit wird der Prozess relativ einfach sein, weil youll wissen, welches Ergebnis Sie zugreifen möchten, werden Sie in der Liste suchen, um herauszufinden, welcher Name es unter gespeichert wird, anstatt auf der Liste und versuchen, herauszufinden, was jeder Element ist. Wie Sie sich vorstellen können, verschiedene Befehle und sogar der gleiche Befehl mit verschiedenen Optionen, speichern Sie verschiedene Ergebnisse. Im Folgenden fassen wir die Variable wieder zusammen, fügen aber die Detailoption hinzu. Dann verwenden wir return Liste, um die Liste der zurückgegebenen Ergebnisse zu erhalten. So wie die Detailoption zusätzliche Informationen zur Ausgabe hinzufügt, führt dies auch zu zusätzlichen Informationen, die in den zurückgegebenen Ergebnissen gespeichert sind. Die neue Liste enthält alle Informationen, die durch den obigen Summen-Befehl zurückgegeben werden, plus Schiefe-Kurtosis und eine Anzahl von Perzentilen, einschließlich der ersten (r (p25)) und dritten (r (p75)) Quarteile und des Medians (r (p50) ). Nun, da wir einen gewissen Sinn dafür haben, welche Ergebnisse durch das zusammengefasste Kommando zurückgegeben werden, können wir die zurückgegebenen Ergebnisse nutzen. Nach dem Durchlaufen eines der oben erwähnten Beispiele werden wir mittig die Variable lesen. Unter der Annahme, dass der letzte Befehl, den wir liefen, der oben zusammengefasste Befehl war, erzeugt der untenstehende Code eine neue Variable, cread, die die mittleren zentrierten Werte von read enthält. Beachten Sie, dass anstatt den tatsächlichen Wert des gelesenen Mittelwerts in diesem Befehl zu verwenden, der Name des zurückgegebenen Ergebnisses (dh r (Mittelwert)) verwendet wird, dann weiß Stata, wann r (Mittelwert) den tatsächlich gespeicherten Wert darstellt Diese Systemvariable. In der nächsten Zeile fassen wir die neue Variable zusammen. Während der Mittelwert nicht genau gleich Null ist, liegt er innerhalb des Rundungsfehlers von Null, so daß wir wissen, daß wir richtig zentriert die Variable gelesen haben. Wie der obige Code vorschlägt, können wir zurückgegebene Ergebnisse ziemlich genau so verwenden, wie wir eine tatsächliche Zahl verwenden würden. Das liegt daran, dass Stata die r (.) Als Platzhalter für einen reellen Wert verwendet. Für ein anderes Beispiel, sagen wir, dass wir die Varianz des Lesens aus seiner Standardabweichung berechnen wollen (ignoriert die Tatsache, dass summarize gibt die Varianz in r (Var)). Wir können dies über den Display-Befehl als Taschenrechner. Die zweite Zeile des Codes unten tut dies. Wir können sogar das Ergebnis überprüfen, indem wir den Wert der Standardabweichung vom Ausgang ausschneiden und einfügen, was im dritten Befehl unten erfolgt. Die Ergebnisse sind grundsätzlich gleich, die geringe Differenz ist Rundungsfehler, da die gespeicherte Schätzung r (sd) mehr Ziffern der Genauigkeit enthält als der Wert der Standardabweichung, der in der Ausgabe angezeigt wird. Arten der zurückgegebenen Ergebnisse, r-Klasse und e-Klasse Nun, da Sie ein wenig über die zurückgegebenen Ergebnisse wissen und wie sie arbeiten, sind Sie bereit für ein wenig mehr Informationen über sie. Zurückgegebene Ergebnisse kommen in zwei Haupttypen, r-Klasse und e-Klasse (es gibt auch s-class und c-class resultsvariables, aber wir werden hier nicht diskutieren). Befehle, die Schätzungen durchführen, zB Regressionen aller Typen, Faktorenanalyse und anova, sind e-class Befehle. Andere Befehle, z. B. Zusammenfassungs-, Korrelations - und Nachschätzbefehle, sind r-class-Befehle. Die Unterscheidung zwischen Befehlen der R-Klasse und der E-Klasse ist wichtig, da Stata Ergebnisse aus Befehlen der e-Klasse und der r-Klasse in verschiedenen quotplaces. quot-Dateien speichert. Dies hat zwei Verzweigungen für Sie als Benutzer. Zuerst müssen Sie wissen, ob die Ergebnisse in r () oder e () (sowie den Namen des Ergebnisses) gespeichert sind, um sie nutzen zu können. Wenn Sie nicht sicher, welche Klasse ein Befehl youve ausgeführt wird, können Sie entweder schauen Sie es in der Hilfedatei oder quotlookquot an einem Ort (mit dem entsprechenden Befehl, um Ergebnisse zu listen), wenn die Ergebnisse nicht dort gespeichert sind sie wahrscheinlich in das andere. Eine potenziell wichtigere Verzweigung des Unterschieds, wie Ergebnisse von r-class - und e-class-Befehlen zurückgegeben werden, besteht darin, dass die zurückgegebenen Ergebnisse nur dann gespeichert werden, wenn ein anderer Befehl derselben Klasse ausgeführt wird. Das heißt, die Ergebnisse der vorherigen Befehle werden durch nachfolgende Befehle derselben Klasse ersetzt. Im Gegensatz dazu wird das Ausführen eines Befehls einer anderen Klasse die zurückgegebenen Ergebnisse nicht beeinflussen. Wenn ich zum Beispiel eine Regression und dann eine zweite Regression ausführe, werden die Ergebnisse der ersten Regression (in e ()) durch diejenigen für die zweite Regression ersetzt (die ebenfalls in e () gespeichert sind). Wenn aber statt einer zweiten Regression ein Nachschätzungsbefehl ausgeführt wurde, würden die Ergebnisse der Regression in e () verbleiben, während die Ergebnisse des Postschätzbefehls in r () platziert werden würden. Zwar gibt es eine Unterscheidung zwischen den beiden, die tatsächliche Verwendung von Ergebnissen aus r-Klasse und e-Klasse Befehle ist sehr ähnlich. Für den Anfang, die Befehle sind parallel, um die r-Klasse Ergebnisse im Speicher zu finden, ist der Befehl Rückkehr Liste. Um das gleiche für E-Klasse Ergebnisse der Befehl ereturn Liste zu tun. Ferner werden mit Ausnahme der Unterschiede in den Benennungskonventionen (r () vs e ()) die Ergebnisse auf die gleiche Weise abgerufen. Das Beispiel unten zeigt dies, zuerst regressieren wir auf weiblich und lesen. Und verwenden Sie dann ereturn Liste, um auf die zurückgegebenen Ergebnisse zu schauen. Die Liste der zurückgegebenen Ergebnisse für regress enthält mehrere Arten von zurückgegebenen Ergebnissen unter den Überschriften Skalaren, Makros, Matrizen und Funktionen. Wir werden die Arten der zurückgegebenen Ergebnisse unten zu diskutieren, aber jetzt werden wir zeigen, wie Sie die skalar zurückgegebene Ergebnisse auf die gleiche Weise, wie wir die zurückgegebenen Ergebnisse von zusammengefasst verwenden können. Eine Möglichkeit zum Berechnen der Varianz der Fehler nach einer Regression besteht beispielsweise darin, die restliche Summe von Quadraten durch die Gesamtfreiheitsgrade (d. h. n-1) zu teilen. Die restliche Summe von Quadraten wird in e (rss) gespeichert, und das n für die Analyse wird in e (N) gespeichert. Im Folgenden verwenden wir den Anzeigebefehl als Taschenrechner zusammen mit den zurückgegebenen Ergebnissen, um die Varianz der Fehler zu berechnen. Die Ergebnisse werden zurückgegeben: Skalare, Zeichenketten, Matrizen und Funktionen Wie oben erwähnt, gibt es sowohl für r - als auch für e-class-Befehle mehrere Typen zurückgegebener Ergebnisse, einschließlich Skalaren, Zeichenketten, Matrizen und Funktionen. In den Listen der zurückgegebenen Ergebnisse ist jeder Typ unter seiner eigenen Position aufgelistet. Die Ergebnisse unter der Überschrift quotscalarsquot sind nur, dass ein einzelner numerischer Wert. Ihre Verwendung wird oben diskutiert, so dass wir nicht mehr über sie in diesem Abschnitt sagen. Zurückgegebene Ergebnisse unter quotmacrosquot sind in der Regel Zeichenfolgen, die Informationen über den Befehl, der ausgeführt wurde, geben. Zum Beispiel enthält e (cmdline) in den zurückgegebenen Ergebnissen für die oben gezeigte Regression den Befehl, den der Benutzer ausgegeben hat (ohne Abkürzungen). Diese werden im Allgemeinen bei der Programmierung von Stata verwendet. Ergebnisse, die unter quotmatricesquot aufgelistet werden, sind, wie Sie erwarten, Matrizen. Während die Liste der Ergebnisse, die von return-Liste und erturn Liste zurückgegeben werden, zeigen Sie die Werte, die von den meisten der zurückgegebenen Ergebnisse, das ist nicht praktisch mit Matrizen, stattdessen die Dimensionen der Matrizen aufgeführt sind. Um den Inhalt von Matrizen zu sehen, müssen Sie sie mit Matrixbefehlen anzeigen lassen. Wir führen dies unten mit der Koeffizientenmatrix (e (b)) unter Verwendung der Befehlsmatrixliste e (b) durch. (Beachten Sie, dass es nach dem Anpassen eines Modells einen anderen Weg zum Zugriff auf Koeffizienten und deren Standardfehler gibt.) Wenn wir Matrixoperationen auf zurückgegebenen Matrizen durchführen oder auf einzelne Elemente der Matrix zugreifen wollen, Verschieben Sie die Matrix als Rückgabewert auf eine normale Matrix. Dies geschieht in der letzten Zeile der Syntax unten. Schließlich werden die Ergebnisse, die unter der Überschrift "Funktionen" wiedergegeben werden, Funktionen enthalten, die in ähnlicher Weise wie andere Stata-Funktionen verwendet werden können. Die am häufigsten von den Stata-Schätzbefehlen zurückgegebene Funktion ist wahrscheinlich e (sample). Diese Funktion markiert das Beispiel, das für die Abschätzung der letzten Analyse verwendet wird. Dies ist nützlich, da Datensätze oft fehlende Werte enthalten, die dazu führen, dass nicht alle Fälle des Datensatzes in einer gegebenen Analyse verwendet werden. Unter der Annahme, dass der letzte Schätzbefehlslauf die Regression des Schreibens auf das Weibchen war, und das Lesen, das oben gezeigt ist, verwendet die erste Codezeile unten e (Stichprobe), um den Mittelwert des Lesens unter den in dem Modell verwendeten Fällen zu finden. Die zweite Codezeile verwendet e (sample), um eine neue Variable namens flag zu erstellen, die für Fälle gleich ist, die in der Analyse verwendet wurden, andernfalls Null. (Anmerkung, da der Beispiel-Dataset keine fehlenden Daten enthält, werden alle Fälle in die Analyse einbezogen, und Flag ist eine Konstante gleich Eins.) Koeffizienten und ihre Standardfehler Wie oben diskutiert, passt ein Modell, Koeffizienten und deren Standard Fehler werden in e () in Matrixform gespeichert. Diese Matrizen ermöglichen dem Benutzer Zugriff auf die Koeffizienten, aber Stata bietet Ihnen einen noch einfacheren Zugriff auf diese Informationen, indem er sie in den Systemvariablen b und se speichert. Um auf den Wert eines Regressionskoeffizienten nach einer Regression zuzugreifen, muss man nur bvarname eingeben, wobei varname der Name der Prädiktorvariablen ist, deren Koeffizient Sie untersuchen möchten. Um auf den Standardfehler zuzugreifen, können Sie einfach sevarname eingeben. Um auf den Koeffizienten und den Standardfehler der Konstante zuzugreifen, verwenden wir Bcons bzw. Sek. Darunter führen wir das gleiche Regressionsmodell, das wir oben liefen (das Auslassen der Ausgabe), mit weiblichen und lesen, um das Schreiben vorherzusagen. Sobald wir das Modell geschätzt haben, verwenden wir den Anzeigebefehl, um zu zeigen, dass die Werte in b gleich unseren Regressionskoeffizienten sind. Schließlich berechnen wir den vorhergesagten Wert des Schreibens, wenn ein weiblicher Student (weiblich 1) eine Leserate von 52 hat. Der Inhalt dieser Website sollte nicht als eine Bestätigung irgendeines bestimmten Webseiten-, Buch - oder Softwareprodukts durch die University of California. An Einführung in Mata Mata ist eine Matrix-Sprache in Stata gebaut, ähnlich in vielerlei Hinsicht zu R, Matlab oder GAUSS. Es hat einige einzigartige und faszinierende Funktionen jedoch. Einer ist, dass es eine kompilierte Sprache anstatt interpretiert wird, was die Leistung verbessert. Es wurde auch in StataMP (verfügbar auf allen SSCC Linux-Servern und Condor) parallelisiert, was die Performance dramatisch verbessert. Auf der anderen Seite Mata ist ziemlich neu und hat noch nicht zitiert onquot an der SSCC, so dass wir keine realen Vergleiche zu bieten haben. Mata ist kein Ersatz für Stata, noch soll es ein eigenständiges statistisches Paket sein. Es ist ein Werkzeug, das am besten als Ergänzung zu Stata (oder SAS oder SPSS) verwendet wird, für diese Dinge tun, macht Stata nicht gut für sich. Insbesondere funktioniert Mata nicht im Rahmen eines einzigen Datensatzes, was ihm zusätzliche Flexibilität bietet. Aber Sie sollten nicht versuchen, Mata zu lernen, es sei denn, Sie sind bereits mit Stata oder einem anderen statistischen Paket vertraut. Mata ist eine relativ flache levelquot-Sprache. Viel von Ihrer Zeit in Stata (oder SAS oder SPSS) wird mit eingebauten Programmen verbracht, finden Sie genau die richtige Kombination von Optionen, um Stata zu tun, was Sie wollen. In Mata werden Sie die direkte Kontrolle übernehmen und Mata sagen, was Sie Schritt für Schritt tun möchten. (Der Mata-Optimierer, auf den wir ausführlich eingehen werden, ist eine bemerkenswerte Ausnahme.) Das bedeutet, dass einfache Dinge in Mata üblicherweise schwerfälliger sind als in Stata, aber Mata hat weniger Einschränkungen. Dieser Artikel ist in erster Linie für Menschen, die erhebliche Erfahrung mit Stata, SAS oder SPSS-Syntax geschrieben haben geschrieben, aber keine anderen Programmiersprachen. So wird es viel Wert auf das Lernen, wie man nützliche Dinge durch die Manipulation von Matrizen zu tun, und viele der Beispiele sind so konzipiert, um Erfahrungen zu machen sowie ein bestimmtes Konzept illustrieren. Können Matlab und GAUSS Veteranen finden, dass sie diese Abschnitte schöpfen können und konzentrieren auf, was zu ihnen neu ist. C-Programmierer finden, dass Mata C imitiert, wann immer es kann, so dass sie wahrscheinlich die Abschnitte auf Standard-Programmierung Konstrukte wie Schleifen. Aber egal, was Ihr Hintergrund, werden Sie lernen, viel mehr, wenn Sie diesen Artikel auf dem Computer lesen, mit Stata laufen, und tatsächlich geben Sie in den Beispielen. Mata läuft innerhalb von Stata, also um Mata zu benutzen, müssen Sie wissen, wie man ein Stata-Programm ausführt, genannt do-Datei. Wenn Sie Stata nie benutzt haben, lesen Sie bitte den Abschnitt über do-Dateien in einer Einführung in Stata. Interaktive Stata (d. H. Starten Sie es und geben Sie in Befehle) ist eine großartige Möglichkeit zu lernen, und das ist, wie youll die Beispiele in diesem Artikel. Aber für echte Arbeit youll wollen alles in do Dateien schreiben. Es gibt mehrere Beispieldateien, die diesem Artikel zugeordnet sind. Es gibt Links zu ihnen im Text, wie sie verwendet werden. Allerdings, wenn youre mit einem SSCC Linux-Server können Sie sie alle vor der Zeit zu kopieren. Geben Sie dazu Folgendes an der Linux-Eingabeaufforderung ein: mkdir mataclass cd mataclass cp usrglobalwebssccpubsfiles4-26. Die meisten der Befehle youll Typ, wie Sie lesen, sind auch in Mataclass. do enthalten. Die beiden wichtigsten Beispiele sind in ex1.do und ex2.do gefunden. Mata Grundlagen Um Mata zu starten, starten Sie zuerst Stata und geben Sie dann das Befehlsfenster ein. Stata schaltet dann auf quotMata mode. quot Seine leicht zu vergessen, welche Modus youre in, aber wenn Sie sorgfältig an der Unterseite des Review-Fenster, wo Sie normalerweise sehen einen Zeitraum youll sehen jetzt einen Doppelpunkt. Natürlich, wenn Sie zum ersten Mal starten Mata theres eine große horizontale Linie, die mata sagt, aber das wird nicht für lange sichtbar sein. Dieser Balken erinnert Sie auch noch an einen anderen nützlichen Befehl: Um aus dem Mata-Modus herauszukommen, während "Stata" um Befehle herum organisiert ist, wird Mata um quotstatements. quot herum organisiert. Zum Beispiel können Sie einfach eingeben und Mata wird zurückgeben, um die Ergebnisse in einer Variablen zu speichern Genauso einfach: Beachten Sie, dass es diesmal keine Ausgabe gab. Wenn Sie den Wert von x sehen wollen, geben Sie einfach: und youll erhalten 4 zurück. Matrix-Operatoren In unseren früheren Beispielen fungierte das Plus-Zeichen als Operator: Mata erkannte es als Sprechen quottake die Sache vor dem Plus-Zeichen und die Sache nach dem Plus-Zeichen und Summe sie. quot Mata definiert auch Matrixoperatoren, die Matrixmanipulationen durchführen. Spalten - und Zeilenverknüpfung Das Komma ist definiert als der Querspalten-Joinquotoperator oder quottake die Sachen vor und nach dem Komma und setzt sie nebeneinander. Das bedeutet, dass Stata als Matrix mit einer Zeile und zwei Spalten interpretiert wird. In der Tat, geben Sie es und die Ausgabe wird: Der Backslash (nicht die Division ist) ist die quotrow joinquot Operator, oder quottake die Sache vor dem Backslash und Stapel es oben auf die Sache nach it. quot So ist eine Matrix mit Zwei Zeilen und eine Spalte: Die zu betreibenden Dinge sind nicht auf Skalare beschränkt, so dass Sie Matrizen konstruieren können. Fehlermeldungen Es gibt Grenzen: ltistmtgt: 3200 Konformitätsfehler r (3200) Sie können nicht eine Matrix, deren erste Zeile hat zwei Spalten, aber deren zweite und dritte Zeile haben eine Spalte. Stata nennt dies einen Konformitätsfehler, eine Klasse, die auch Dinge beinhaltet, die versuchen, Matrizen zu multiplizieren, wobei die Anzahl der Spalten der ersten Matrix von der Anzahl der Zeilen der zweiten unterscheidet. Dies ist auch ein Fehlerquote-Fehler. Wenn Sie Mata eine Anweisung geben, ist das erste, was sie tut, die Anweisung in den Bytecode zu kompilieren. An diesem Punkt Mata nicht Blick auf die spezifischen Mengen manipuliert werden. Alles, was sie sieht, ist Zitat etwas neben etwas anderes, dann stapeln, die auf zwei anderen things. quot Das macht Sinn, so dass die Anweisung erfolgreich kompiliert. Erst wenn der Code läuft, merkt Mata, dass die zu stapelnden Dinge unterschiedliche Spaltenzahlen haben. Ein Kompilierungszeitfehler wird generiert, wenn die Anweisung keinen Sinn macht, egal in welcher Menge Sie es anschließen. Beispielsweise sind ungültiger Ausdruck r (3000) Kompilierzeitfehler immer Fehlercode 3000 und enthalten nicht ltistmtgt. Beachten Sie, ob ein Fehler ein Kompilierfehler ist oder ein Laufzeitfehler kann Ihnen bei der Suche des Problems helfen. Wenn es Laufzeit war, wissen Sie, Mata zumindest verstanden Ihren Code (obwohl theres keine Garantie es verstanden, dass es bedeutet, dass die gleiche Sache, die Sie verstehen, es bedeuten) und es war etwas über die spezifischen Mengen, mit denen es das Problem verursacht wurde. Wenn seine Kompilierzeit, macht der Code nicht einmal Sinn, Mata. Klammern Als youre Zusammenstellung Matrizen sind Sie willkommen, um Klammern zu helfen, Dinge zu klären. Seine meist zu Ihrem Nutzen: sind alle gleich, Mata, aber wenn einer von ihnen sieht klarer zu Ihnen mit allen Mitteln verwenden, dass Stil in allen Programmen. (Ich bin zufällig ein Fan des dritten.) Range Operators Eine weitere Reihe von praktischen Tools für die Erstellung von Matrizen sind die Bereich Operatoren. Das. Erzeugt eine Reihe beginnend von der Zahl auf der linken Seite bis zur Zahl auf der rechten Seite und macht sie zu einem Zeilenvektor. Das. Operator tut dasselbe, setzt sie aber in einen Spaltenvektor. Sein nicht begrenzt auf Ganzzahlen, jedoch: Beachten Sie, dass die Zahl auf der rechten Seite ist nicht garantiert Teil der resultierenden Serie sein. Die Ergebnisse von Matrix-Anweisungen können natürlich ebenso wie andere Anweisungen in Variablen gespeichert werden. Zum Beispiel: Beachten Sie, wie die Definition von z sieht viel wie die Aussage, die wir früher versucht, gab einen Laufzeitfehler. Es läuft jetzt, weil x und y beide haben zwei Spalten. Deshalb war es ein Laufzeitfehler und kein Kompilierzeitfehler: Angesichts der richtigen Eingaben kann die Anweisung funktionieren. Arithmetische Operatoren Die arithmetischen Standardoperatoren erkennen, wann eines oder beide ihrer Argumente eine Matrix ist, und handeln entsprechend. Dazu gehören Addition und Subtraktion, skalare Zeiten Matrix, und vollständige Matrix-Multiplikation. Es gibt jedoch auch quotcolonquot-Operatoren, die elementweise arbeiten. Für Addition und Subtraktion macht es keinen Unterschied, da sie Element von Element sowieso. Aber es macht einen großen Unterschied mit der Multiplikation. Beachten Sie Folgendes: Logische Operatoren Logische Operatoren wie z. B. größer als, kleiner und gleich sind auch in Matrix - und Doppelpunkt-Versionen definiert (beachten Sie, dass der Operator equals im Gegensatz zur Zuweisung ist). Mata verfügt jedoch nicht über einen booleschen Variablentyp. Somit geben logische Operatoren (wie in Stata) eine für true und null für false zurück. Wenn Sie zwei Matrizen mit Standard größer als, kleiner oder gleich vergleichen, gibt Mata genau dann eine Zahl zurück, wenn die Bedingung für alle Elemente der beiden Matrizen zutrifft. Andernfalls gibt sie Null zurück Diese Operatoren können nur auf Matrizen derselben Größe arbeiten. In ihrer Kolonform ist das Ergebnis eine Matrix, die alle Element-für-Element-Vergleiche enthält. Die Kolonform ermöglicht mehr Flexibilität hinsichtlich der Größen der zu vergleichenden Matrizen. Wenn ein Argument ein Skalar-, Zeilenvektor oder Spaltenvektor ist, wird es nach Bedarf dupliziert, so dass es mit der anderen Matrix übereinstimmt. Der transpose-Operator ist das richtige einfache Anführungszeichen (). Es funktioniert auf nur einem Objekt, das eine auf der linken Seite, und gibt seine Transponierung zurück: Es gibt jedoch keinen inversen Operator. Die Inversion erfolgt durch Funktionen. Subscripts Um ein bestimmtes Element einer Matrix zu verweisen, setzen Sie die Zeile und Spaltennummer in eckige Klammern nach dem Matrixnamen. Bei der oben definierten Matrix x ist x1,1 1. x1,2 ist 2. usw. Subscripts können auf jeder Seite eines Gleichheitszeichens verwendet werden: Wenn eine Matrix ein Zeilen - oder Spaltenvektor ist, können Sie nur einen Index verwenden. Mißbrauchsquot für die Zeilen - oder Spaltennummer bedeutet alle Zeilen oder Spalten. Sie können entweder den Standard-Zeitraum für fehlende oder einfach verlassen Sie die Nummer aus. So können Sie auch jede Zahl durch einen Zeilen - oder Spaltenvektor ersetzen (und Stata kümmert sich nicht um die von Ihnen verwendete). Allerdings sind diesmal Klammern erforderlich, damit Mata weiß, wo die Liste der Zeilen endet und die Spalten beginnen. Sie können Zeilen oder Spalten wiederholen, die Reihenfolge verarbeiten, oder sogar Matrizen erzeugen, die größer als das Original sind. Vordefinierte Vektoren sind ebenfalls gut: Beachten Sie, dass die Verwendung von Vektor-Indizes wie diese schneller ist als das Multiplizieren mit einer Permutationsmatrix. Subskriptionsbereiche Mata bezieht sich auf die Subscripts, die wir bisher als quotlistquot-Indizes verwendet haben. Jedes Element, das wir wollten, wurde explizit aufgelistet (mit Ausnahme der fehlenden Bedeutung quotallquot). Aber youll wollen oft einen Bereich von Zeilen und Spalten angeben. Theres nicht viel Bedarf für Bereiche mit kleinen Matrizen, so beginnen mit der Schaffung einer größeren Matrix: Nehmen Sie sich einen Moment Zeit, um herauszufinden, wie dies funktioniert, bevor Sie weiter lesen. Die erste Zeile definiert x1 als Zeilenvektor mit dem Bereichsoperator. Der zweite definiert einen x2-Spaltenvektor, der wiederum den Bereichsoperator verwendet, aber das Ergebnis mit zehn multipliziert. Der dritte definiert dann x als zehn Kopien von x1, die übereinander gestapelt sind, plus zehn Kopien von x2, die nebeneinander angeordnet sind. Das Ergebnis ist eine zehn mal zehn Matrix, die von 11 bis 110 geht, was es leicht macht, zu ermitteln, welche Zeilen und Spalten in den nächsten Beispielen extrahiert wurden. (Ja, es gibt einfachere Möglichkeiten, dies zu tun mit Funktionen andor Schleifen, aber wir havent bedeckt sie noch.) Eine Methode für das Extrahieren einer Reihe von Zeilen und Spalten aus x ist die Bereichsoperatoren in der Liste Indizes verwenden. Damit Sie die Zeilen 3-7 und die Spalten 4-8 erhalten, können Sie Folgendes tun: Dies entspricht genau dem Tippen: True-Range-Indizes sind jedoch unterschiedlich. Zum einen sind sie in eckigen Klammern und dem Pipe-Character (shift-backslash) enthalten. Es gibt zwei Elemente, die durch einen Backslash getrennt sind: die Zeile und Spalte der oberen linken Ecke des gewünschten Bereichs und die untere rechte Ecke des gewünschten Bereichs. So ist das Äquivalent zu Denken davon als Ersatz für vier bis sieben und Spalten drei bis achtzig mit quoteverything zwischen Zeile vier, Spalte drei und Zeile sieben, Spalte acht, inclusive. quot Fehlen kann mehrere Dinge in Bereich Indizes bedeuten. Bei der Angabe der oberen linken Ecke bedeutet dies entweder die erste Zeile oder die erste Spalte. Wenn sie in der unteren rechten Ecke verwendet wird, bedeutet dies entweder die letzte Zeile oder die letzte Spalte. Also ist einfach alle x. Theres auch keine Regel, die besagt, dass die oberen linken und unteren rechten Ecken nicht auf der gleichen Zeile oder Spalte sein können. Dies bedeutet, drei Drittel, von der dritten Spalte bis zum Ende. quot Ihre Daten von Stata zu Mata Die meiste Zeit werden Sie nicht die Schaffung von Matrizen von Hand. Stattdessen wollen Sie Matrizen mit den Daten, die Sie bereits in Stata zu machen. Youre auch wahrscheinlich, um Ihre Mata Ergebnisse zu nehmen und sie zu Ihrem Stata-Datensatz zu kopieren. Die erste Frage, die Sie beantworten müssen, ist, ob Sie eine neue Kopie Ihrer Daten erstellen wollen oder ob Sie Mata direkt mit den Stata-Daten arbeiten möchten. Mata kann Matrizen definieren, die eigentlich quotationsquot Ihrer Stata-Daten sind. Denken Sie an eine Ansicht als nur geben Sie Ihre Stata-Daten einen Namen, den Mata verwenden kann. Ansichten verwenden eine triviale Menge an Speicher, auch wenn Ihre Daten sehr groß sind. Wenn Sie die Tabellen einer View-Matrix ändern, werden auch die Stata-Daten geändert. Wenn Sie eine Kopie der Daten statt, können Sie Änderungen vornehmen und halten Sie die ursprüngliche intakt, aber youll verwenden doppelt so viel Speicher. Um Kopien Ihrer Daten zu erstellen, verwenden Sie die Funktion stdata. Die einfachste und die schnellste ist stdata. Es wird verwendet, um eine einzelne Zahl aus Ihrem Datensatz zu erhalten. Es braucht zwei Argumente: eine Beobachtungsnummer und eine variable Zahl. Denken Sie an Ihren Datensatz als Matrix bereits, und die Variable Nummer ist einfach die Spaltennummer der Variablen, die Sie wollen. Um dies in Aktion sehen, erste Ende Mata durch Eingabe Ende. Laden Sie den Auto-Beispiel-Datensatz, Liste Beobachtung ein, und gehen Sie zurück in Mata: Ende sysuse auto l in 1 mata Jetzt erhalten Sie den Preis für das erste Auto durch Eingabe: Das Ergebnis wird 4099, was entspricht, was Sie gerade aufgeführt. Versuchen Sie einige andere Variablen, um sicherzustellen, dass Sie die Idee bekommen haben. Beachten Sie, dass, wenn Sie versuchen, die erste Spalte youll erhalten einen fehlenden Wert zu erhalten. Das ist, weil stdata nur für numerische Daten ist. Für Strings verwenden Sie stsdata, die in ziemlich viel die gleiche Weise funktioniert. Mata kann die Spaltennummern für Sie mit der Funktion stvarindex nachschlagen. Er nimmt den Namen der Variablen an und gibt den Index zurück. Allerdings wird dieser Prozess verlangsamt die Dinge ein wenig. Beachten Sie, dass Sie die Ausgabe einer Funktion als Argument für eine andere Funktion verwenden können: Wenn Sie mehr als einen Wert verwenden möchten, verwenden Sie stdata. Stdata kann wie stdata verwendet werden (obwohl in diesem Fall stdata schneller ist). Stdata ist jedoch flexibler über die Argumente, die es akzeptieren wird. Die Zeilennummer kann fehlen, in diesem Fall werden alle Beobachtungen zurückgegeben. Es kann auch ein Spaltenvektor sein, wobei nur die angegebenen Beobachtungen zurückgegeben werden. Es kann auch eine Matrix mit zwei Spalten sein. Jede Zeile repräsentiert dann einen Bereich von Beobachtungen. Eine fehlende wird in diesem Fall als letzte Beobachtung interpretiert. Die Spaltennummer kann auch fehlen (alle Variablen) oder ein Vektor mit den gewünschten Variablen. Es kann auch die Namen der Variablen anstatt ihre Zahlen akzeptieren, obwohl dies langsamer sein wird. Es ist nicht erlaubt, Bereiche wie Sie für Zeilen angeben können. Wie stdata. Stdata kann keine Strings verarbeiten. Aber es gibt ein äquivalentes stsdata. Versuchen Sie folgendes: stdata (.) Stdata ((1,3), 2) stdata (1, (2,4)) stdata (1, (quotpricequot, quotrep78quot)) stsdata (., 1) In Matrizen für die zukünftige Verwendung gespeichert. Stdata kann auch ein drittes Argument: eine Selektionsvariable. Dies kann entweder der Index oder der Name einer Variablen sein, und wenn es angegeben ist, werden nur Beobachtungen zurückgegeben, bei denen diese Variable ungleich Null ist. Die Auswahl von 0 als Auswahlvariable hat eine besondere Bedeutung: In diesem Fall werden Beobachtungen ausgeschlossen, wenn sie für jede angegebene Variable einen fehlenden Wert haben. Stview macht Ansichten auf Ihre Daten, anstatt sie zu kopieren, aber Sie können Zeilen und Variablen mit den exakt gleichen Methoden wie stdata auswählen. Stview gibt Ihnen die Ergebnisse jedoch auf eine andere Weise. Mit stdata. Die Matrix, die Sie wollten, war, was die stdata Funktion zurückbrachte. Sie können dann die Ergebnisse speichern oder einfach auf dem Bildschirm anzeigen lassen. Mit Blick. Die Funktion selbst gibt nichts zurück. Stattdessen müssen Sie eine Matrix übergeben, die durch die ausgewählte Ansicht ersetzt wird. Ein Fang ist, dass die Matrix existieren muss, bevor Sie es passieren. Es spielt keine Rolle, wie groß es ist - in der Tat wird es komplett ersetzt werden - aber wenn Sie versuchen, eine Matrix an Stview, die nicht irgendwie youll definiert definiert haben Ein Laufzeitfehler. Eine einfache Möglichkeit ist, einfach die Matrix, die Sie verwenden möchten, auf Null, bevor Sie es in. Sie können sogar tun, dass in Ihrem Funktionsaufruf: x und n sehen jetzt wie eine regelmäßige Matrizen, aber denken Sie daran, dass sie tatsächlich Ansichten sind . Wenn Ihr Ziel bei der Verwendung einer Ansicht ist, Speicher zu speichern, müssen Sie darauf achten, keine Kopien davon zu machen. Erstellt keine neue Ansicht. Es erzeugt eine neue Matrix mit den Werten von x eine Kopie. Subtiler erzeugt eine Kopie der j-ten Spalte von x. Wenn Sie eine bestimmte Spalte von x benötigen, können Sie einfach eine neue Ansicht erstellen: Es gibt keine Begrenzung für die Anzahl der verschiedenen Ansichten, die Sie für dieselben Daten einrichten können. Abrufen von Daten aus Mata nach Stata Wenn Sie nur die Werte der vorhandenen Variablen ändern, eine Ansicht erstellen und beliebig ändern möchten. Aber wenn Sie neue Variablen hinzufügen wollen, brauchen Sie ein paar weitere Funktionen. Staddvar fügt eine neue Variable hinzu. Es braucht zwei Argumente: den Variablentyp und den Variablennamen. Es gibt den Index der Variablen zurück, obwohl die Speicherung für zukünftige Verwendung optional ist. Ermöglicht eine neue Variable: weightmpg (also, Pfund-Meilen pro Gallone, eine Einheit, die nur ein Ingenieur lieben könnte). Beachten Sie, dass die Matrix mpg nun eine Ansicht aller Zeilen der mpg-Variablen ist. Um diese neue Variable mit den Stata-Werkzeugen zu erforschen, die Sie vertraut sind, geben Sie ende ein. Natürlich ist dies ein außerordentlich ungeschicktes Ersatz für lange pmgmpgweight Dies führt zu einem allgemeinen Prinzip: Verwenden Sie Mata für das, was seine gut, und Stata für das, was seine gut. Es gibt eine Vielzahl von Funktionen, die Sie für die Kommunikation zwischen Mata und Stata verwenden können, einschließlich Hinzufügen von Beobachtungen und dem Festlegen lokaler Makros, e () - und r () - Vektoren und vieles mehr. Einzelheiten hierzu finden Sie im Handbuch. Speichern und Laden von Mata-Daten Am häufigsten ist das Ziel eines Mata-Programms, Ihre Stata-Daten zu nehmen, ein Ergebnis zu finden und dann entweder anzuzeigen oder wieder in Ihre Stata-Daten zu setzen. Allerdings können Sie Mata-Matrizen selbst speichern, wenn Sie müssen. Um eine Matrix zu speichern, geben Sie mata matsave ein. Filename matrixlist Ersetzen Sie den Dateinamen durch den Namen der Datei, die Sie erstellen möchten. Stata wird. mmat zum Dateinamen automatisch hinzufügen. Ersetzen Sie die Matrixliste durch eine oder mehrere Matrizen, die Sie speichern möchten (Trennung mit Leerzeichen) oder mit einem Sternchen (), um alle derzeit im Speicher befindlichen Matrizen zu speichern. Um die zuvor gespeicherten Matrizen zu laden, geben Sie mata matuse filename ein. Dadurch werden alle im Dateinamen gespeicherten Matrizen geladen. Beide Befehle akzeptieren die Ersetzungsoption. Für matsave. Dadurch kann Mata die vorhandene Datei auf der Festplatte überschreiben. Für matuse. Dadurch kann Mata bestehende Matrizen im Speicher mit denselben Namen wie die Matrizen in der Datei überschreiben. Diese Befehle sind für den interaktiven Gebrauch vorgesehen und können nicht in Funktionen verwendet werden. Wenn Sie Matrizen innerhalb einer Funktion speichern müssen, schauen Sie sich fopen an. Fputmatrix. Fgetmatrix. Und 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