⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m1_interactive.hlp

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 HLP
📖 第 1 页 / 共 2 页
字号:

	: {cmd:st_view(y=., ., "mpg")}

	: {cmd:st_view(X=., ., ("weight", "foreign", "cons"))}

{p 4 4 2}
Let us explain.  We wish we could type 

	: {cmd:y = st_view(., "mpg")}

	: {cmd:X = st_view(., ("weight", "foreign", "cons"))}

{p 4 4 2}
because that is what the functions are really doing.  We cannot
because {cmd:st_view()} (unlike all other Mata functions), returns a
special kind of matrix called a view.
A view acts like a regular matrix in nearly
every respect, but views do not consume nearly as much memory, because they
are in fact views onto the underlying Stata dataset!

{p 4 4 2}
We could instead create {cmd:y} and {cmd:X} using Mata's 
{bf:{help mf_st_data:[M-5] st_data()}} function, and then we could type the
creation of {cmd:y} and {cmd:X} the natural way,

	: {cmd:y = st_data(., "mpg")}

	: {cmd:X = st_data(., ("weight", "foreign", "cons"))}

{p 4 4 2}
{cmd:st_data()} returns a real matrix, which is a copy of the data Stata has
stored in memory.

{p 4 4 2}
We could use {cmd:st_data()} and be done with the problem.  For
our automobile-data example, that would be a fine solution.  But were the
automobile data larger, you might run short of memory, and views can save lots
of memory.  You can create views willy-nilly -- lots and lots of them -- and
never consume much memory!  Views are wonderfully convenient and it is worth
mastering the little bit of syntax to use them.

{p 4 4 2}
{cmd:st_view()} requires three arguments:  the name of the view matrix 
to be created, the observations (rows) the matrix is to contain, and the
variables (columns).  If we wanted to create a view matrix
{cmd:Z} containing all the observations and all the variables, we could type

	: {cmd:st_view(Z, ., .)}

{p 4 4 2}
{cmd:st_view()} understands missing value in the second and third positions to
mean all the observations and all the variables.  Let's try it:

	: {cmd:st_view(Z, ., .)}
	                 {err:<istmt>:  3499  Z not found}
        r(3499);

	: {cmd:_}

{p 4 4 2}
That did not work because Mata requires {cmd:Z} to be predefined.  The reasons
are technical, but it should not surprise you that function arguments need to
be defined before a function can be used.  Mata just does not understand that
{cmd:st_view()} really does not need {cmd:Z} defined.  The way around Mata's
confusion is to define {cmd:Z} and then let {cmd:st_view()} redefine it:

	: {cmd:Z = .}

	: {cmd:st_view(Z, ., .)}

{p 4 4 2}
You can, if you wish, combine all that into one statement


	: {cmd:st_view(Z=., ., .)}

{p 4 4 2}
and that is what we did when we defined {cmd:y} and {cmd:X}:

	: {cmd:st_view(y=., ., "mpg")}

	: {cmd:st_view(X=., ., ("weight", "foreign", "cons"))}

{p 4 4 2}
The second argument ({cmd:.}) specified that we wanted all the observations,
and the third argument specified the variables we wanted.  Be careful not to
omit the "extra" parentheses when typing the variables.  Were you to type

	: {cmd:st_view(X=., ., "weight", "foreign", "cons")}

{p 4 4 2}
you would be told you typed an invalid expression.  {cmd:st_view()} expects
three arguments, and the third argument is a row vector specifying the
variables to be selected:  {cmd:("weight",} {cmd:"foreign",} {cmd:"cons")}.

{p 4 4 2}
At this point, we suggest you type 

	: {cmd:y}
	{it:(output omitted)}

	: {cmd:X}
	{it:(output omitted)}

{p 4 4 2} 
to see that {cmd:y} and {cmd:X} really do contain our data.  In case 
you have lost track of what we have typed, here is our complete session so
far:

	. {cmd:sysuse auto}
	. {cmd:gen cons = 1}
	. {cmd:keep mpg weight foreign cons}
	. {cmd:regress mpg weight foreign cons}
	. {cmd:keep if e(sample)}
	. {cmd:replace weight = weight/1000}
	. {cmd:mata}
	: {cmd:st_view(y=., ., "mpg")}
	: {cmd:st_view(X=., ., ("weight", "foreign", "cons"))}


{title:9.  Perform your matrix calculations}

{p 4 4 2}
To remind you, our matrix calculations are

	        {bf:b} = ({bf:X}'{bf:X})^(-1){bf:X}'{bf:y}

	        {bf:V} = {it:s}^2*({bf:X}'{bf:X})^(-1)

	where

              {it:s}^2 = {bf:e}'{bf:e}/({it:n}-{it:k})

		{bf:e} = {bf:y} - {bf:X}*{bf:b}

		{it:n} = rows({bf:X})

		{it:k} = cols({bf:X})

{p 4 4 2}
Let's get our regression coefficients, 

	: {cmd:b = invsym(X'X)*X'y}

	: {cmd:b}
        {res}       {txt}           1
            {c TLC}{hline 16}{c TRC}
          1 {c |}  {res}-6.587886358{txt}  {c |}
          2 {c |}  {res}-1.650029004{txt}  {c |}
          3 {c |}  {res} 41.67970227{txt}  {c |}
            {c BLC}{hline 16}{c BRC}{txt}

{p 4 4 2}
and let's form the residuals, define {it:n} and {it:k}, and 
obtain {it:s}^2,

	: {cmd:e  = y - X*b}
	: {cmd:n  = rows(X)}
	: {cmd:k  = cols(X)}
	: {cmd:s2 = (e'e)/(n-k)}

{p 4 4 2}
so we are able to calculate the variance matrix:

	: {cmd:V = s2*invsym(X'X)}
	: {cmd:V}
        {res}{txt}[symmetric]
                          1              2              3
            {c TLC}{hline 46}{c TRC}
          1 {c |}  {res} .4059128628                              {txt}  {c |}
          2 {c |}  {res} .4064025078    1.157763273               {txt}  {c |}
          3 {c |}  {res}-1.346459802    -1.57131579    4.689594304{txt}  {c |}
            {c BLC}{hline 46}{c BRC}{txt}

{p 4 4 2}
We are done.

{p 4 4 2}
We can present the results in more readable fashion by pulling the diagonal of
{it:V} and calculating the square root of each element:

	: {cmd:se = sqrt(diagonal(V))}
	: {cmd:(b, se)}
        {res}       {txt}           1              2
            {c TLC}{hline 31}{c TRC}
          1 {c |}  {res}-6.587886358    .6371129122{txt}  {c |}
          2 {c |}  {res}-1.650029004    1.075994086{txt}  {c |}
          3 {c |}  {res} 41.67970227    2.165547114{txt}  {c |}
            {c BLC}{hline 31}{c BRC}{txt}

{p 4 4 2}
You know that if we were to type 

	: {cmd:2+3}
	  5

{p 4 4 2}
Mata evaluates the expression and shows us the result, and that is 
exactly what happened when we typed 

	: {cmd:(b, se)}

{p 4 4 2}
{cmd:(b,} {cmd:se)} is an expression, and Mata evaluated it and displayed the
result.  The expression means to form the matrix whose first column is
{cmd:b} and second column is {cmd:se}.  We could obtain a listing of the
coefficient, standard error, and its t statistic by asking Mata to display
{cmd:(b, se, b:/se)},

	: {cmd:(b, se, b:/se)}
        {res}       {txt}           1              2              3
            {c TLC}{hline 46}{c TRC}
          1 {c |}  {res}-6.587886358    .6371129122   -10.34021793{txt}  {c |}
          2 {c |}  {res}-1.650029004    1.075994086   -1.533492633{txt}  {c |}
          3 {c |}  {res} 41.67970227    2.165547114    19.24673077{txt}  {c |}
            {c BLC}{hline 46}{c BRC}{txt}

{p 4 4 2}
In the expression above, {cmd:b:/se} means to divide the elements of {cmd:b} 
by the elements of {cmd:se}.  {cmd::/} is called a colon operator and 
you can learn more about it by seeing 
{bf:{help m2_op_colon:[M-2] op_colon -- Colon operators}}.

{p 4 4 2}
We could add the significance level by typing

	: {cmd:(b, se, b:/se, 2*ttail(n-k, abs(b:/se)))}
        {res}       {txt}           1              2              3              4
            {c TLC}{hline 61}{c TRC}
          1 {c |}  {res}-6.587886358    .6371129122   -10.34021793    8.28286e-16{txt}  {c |}
          2 {c |}  {res}-1.650029004    1.075994086   -1.533492633     .129598713{txt}  {c |}
          3 {c |}  {res} 41.67970227    2.165547114    19.24673077    6.89556e-30{txt}  {c |}
            {c BLC}{hline 61}{c BRC}{txt}

{p 4 4 2}
Those are the same results reported by {cmd:regress}; type 

	. {cmd:sysuse auto}
	. {cmd:replace weight = weight/1000}
	. {cmd:regress mpg weight foreign}

{p 4 4 2}
and compare results.

{title:Review}

{p 4 4 2}
Our complete session was

	. {cmd:sysuse auto}
	. {cmd:gen cons = 1}
	. {cmd:keep mpg weight foreign cons}
	. {cmd:regress mpg weight foreign cons}
	. {cmd:keep if e(sample)}
	. {cmd:replace weight = weight/1000}

	. {cmd:mata}
	: {cmd:st_view(y=., ., "mpg")}
	: {cmd:st_view(X=., ., ("weight", "foreign", "cons"))}

	: {cmd:b = invsym(X'X)*X'y}
	: {cmd:b}
	: {cmd:e = y - X*b}
	: {cmd:n = rows(X)}
	: {cmd:k = cols(X)}
	: {cmd:s2= (e'e)/(n-k)}
	: {cmd:V = s2*invsym(X'X)}
	: {cmd:V}

	: {cmd:se = sqrt(diagonal(V))}
	: {cmd:(b, se)}
	: {cmd:(b, se, b:/se)}
	: {cmd:(b, se, b:/se, 2*ttail(n-k, abs(b:/se)))}
	: {cmd:end}


{title:Also see}

{p 4 13 2}
Manual:  {hi:[M-1] interactive}

{p 4 13 2}
Online:  help for 
{bf:{help mata:[M-0] intro}},
{bf:{help m1_intro:[M-1] intro}}
{p_end}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -