Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: create predicted values manually using matrices


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: Re: create predicted values manually using matrices
Date   Tue, 24 Nov 2009 06:49:08 -0500

<>
On Nov 24, 2009, at 2:33 AM, Joao wrote:

> 
> Sorry, but as I'm understanding the problem here is to use time series
> operators and matrix... To use the time series operators you must
> - -tsset- your data first.
> 
> clear all
> input depvar indepvar1 indepvar2
> 2 15 3
> 3 23 5
> 4 19 8
> 2 32 6
> 5 27 4
> end
> 
> gen time=_n
> tsset time, yearly
> 
> reg depvar indepvar1 indepvar2
> gen cons=1
> foreach var of varlist indepvar1 indepvar2 cons{
> 	gen f`var'=F.`var'
> }
> mkmat findepvar1 findepvar2 fcons, matrix(var_ind)
> matrix B1=e(b)
> matrix y_hat1 = var_ind*B1'
> svmat y_hat1, names(depvar_hat)


A solution using Stata's matrix language may run afoul of the matsize limit, which in Stata/IC is an iimmutable 800 rows or columns. I don't think you are trying to do anything that requires matrices -- as others have posted, predict should be able to do what you need without any explicit algebra -- but if you want to do something like this, use Mata, as Martin suggested. Mata is not subject to the matsize limit. A silly example:

clear
set obs 1000000
g junk = uniform()
tomata junk
mata:
junk = junk :/ colsum(junk :* junk)
end
su junk


As tomata (from SSC, by Bill Gould) creates view matrices, an alternative to Martin's code would be

reg price weight foreign trunk if !mi(rep78)
g double pred = .
g double res = .
tomata price weight foreign trunk pred res if e(sample)
mata:
	b=st_matrix("e(b)")'
	X=(weight, foreign, trunk, J(rows(weight),1,1)) 
	y=price
	pred[.] = X*b
	res[.] = y - pred
end


Kit Baum   |   Boston College Economics & DIW Berlin   |   http://ideas.repec.org/e/pba1.html
                              An Introduction to Stata Programming  |   http://www.stata-press.com/books/isp.html
   An Introduction to Modern Econometrics Using Stata  |   http://www.stata-press.com/books/imeus.html


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index