Hi Leandro, recently I've done alomost the same stuff as you and the 'trick' I use to make the code shorter is the following: (here an example without any real data)
matrix means = zeros(nobs,1)
loop i=1..I --quiet
loop n=1..N --quiet
loop k=1..K --quiet
smpl (variable1 == i && variable2 == n && variable3 == k)
--restrict --replace --balanced
if psd(x) >0
means[i,1] = pmean(x)
endif
endloop
endloop
endloop
One could improve on this further and further...
Perhaps this helps you.
Cheers
Leon
Am 08.06.2011 18:17, schrieb Leandro Zipitria:
> Dear Jack, thanks for your answer.
>
> I will try to be more specific on the second answer.
> I have a panel data of price of several goods (variables) for
> supermarkets (units) at different time. Also, I have information for
> to wich city it belong each supermarket.
>
> I need to calculate several means:
>
> 1) for each good, then for each city, and then for each day, the mean
> price for all supermarkets that post the price that day.
> 2) forget goods for the moment. I first restrict the sample by city,
> and then by time (as I generate a time dummy for each day)
> 3) for each time and city I want to calculate the mean price, so I
> made the following script:
>
> smpl full
>
> genr time
> genr veces = d_prod_1 <> 0
> discrete ciudad
>
> smpl time <3 --restrict
>
> Mciudad = values(ciudad)
> Mtime = values(time)
>
> #matriz para poner las medias de cada estimacion
> matrix Xmean2 = Mciudad ** Mtime'
> matrix Xmean = Xmean2 - Xmean2
>
> #matriz para calcular la media y desviación estandar
> loop i=1..rows(Mciudad)
> scalar xi = Mciudad[i]
> smpl (ciudad=xi) --restrict --replace
> loop j=1..rows(Mtime)
> scalar tj = Mtime[j]
> smpl (time=tj) --restrict --replace
> find = {$i;$j}
> scalar m = mean(veces)
> subst = {m}
> X = replace(Xmean, find, subst)
> printf "mean(veces | ciudad = %g | time = %g) = %8.5f, sd(veces |
> ciudad = %g | time = %g) = %g\n", \
> xi, tj, mean(veces), xi, tj, sd(veces)
> end loop
> end loop
>
> 4) I generate a matrix which rows are the (number of) city and columns
> the time moment.
>
> Mciudad = values(ciudad)
> Mtime = values(time)
>
> #matriz para poner las medias de cada estimacion
> matrix Xmean2 = Mciudad ** Mtime'
> matrix Xmean = Xmean2 - Xmean2
>
> 5) Now I want to store in the ith jth element of the matrix, the mean
> of the ith city at the jht moment of time, so I try this, which is
> wrong I supposse:
>
> find = {$i;$j}
> scalar m = mean(veces)
> subst = {m}
> X = replace(Xmean, find, subst)
>
>
> Is there any way to store the iht jth calculation of the mean in the
> matrix I created in that position?
>
> Thanks, and I hope to be more clear now
> Leandro
>
> 2011/6/6 Riccardo (Jack) Lucchetti <r.lucchetti(a)univpm.it
> <mailto:r.lucchetti(a)univpm.it>>
>
> On Mon, 6 Jun 2011, Leandro Zipitria wrote:
>
> If I am working with a restricted version of a variable (using
> smpl --restrict), the mean, standard deviation, etc. reported
> is for the variable as a whole or just the restricted sample?
>
>
> Just the restricted sample. Example:
>
> gretl version 1.9.5cvs
> Copyright Ramu Ramanathan, Allin Cottrell and Riccardo "Jack"
> Lucchetti
> This is free software with ABSOLUTELY NO WARRANTY
> Current session: 2011-06-06 21:11
>
> "help" gives a list of commands
> Type "open filename" to open a data set
> ? open data4-1
> open data4-1
>
> Read datafile /usr/local/share/gretl/data/data4-1.gdt
> periodicity: 1, maxobs: 14
> observations range: 1-14
>
> Listing 5 variables:
> 0) const 1) price 2) sqft 3) bedrms 4) baths
>
> ? m = mean(sqft)
> m = mean(sqft)
> Generated scalar m = 1910.93
> ? smpl 2 10
> smpl 2 10
> Full data range: 1 - 14 (n = 14)
> Current sample: 2 - 10 (n = 9)
>
> ? m = mean(sqft)
> m = mean(sqft)
> Replaced scalar m = 1670.44
>
>
> As a second answer, I am creating a doble loop for iterating
> mean and standar deviation from a variable. I want to create a
> matix that store the results of each calculation. That is, I
> have a panel, and want to restrict it looping on one variable
> (city) which will be the columns, and the time the rows. Each
> time gretl compute one statistic, I am trying to store it in
> the matrix, following the previous order. Is this possible?
>
>
> Yes it is. That said, I'm not at all sure to have understood what
> your need is, but I have the feeling that you can write a much
> more efficient script by using the pmean() and psd() function.
> Also, the user's guide is your friend.
>
> Riccardo (Jack) Lucchetti
> Dipartimento di Economia
> Università Politecnica delle Marche
>
> r.lucchetti(a)univpm.it <mailto:r.lucchetti(a)univpm.it>
> http://www.econ.univpm.it/lucchetti
> _______________________________________________
> Gretl-users mailing list
> Gretl-users(a)lists.wfu.edu <mailto:Gretl-users(a)lists.wfu.edu>
> http://lists.wfu.edu/mailman/listinfo/gretl-users
>
>
>
> _______________________________________________
> Gretl-users mailing list
> Gretl-users(a)lists.wfu.edu
> http://lists.wfu.edu/mailman/listinfo/gretl-users
|
Hi Leandro, recently I've done alomost the same stuff as you and the 'trick' I use to make the code shorter is the following: (here an example without any real data) matrix means = zeros(nobs,1) loop i=1..I --quiet loop n=1..N --quiet loop k=1..K --quiet smpl (variable1 == i && variable2 == n && variable3 == k) --restrict --replace --balanced if psd(x) >0 means[i,1] = pmean(x) endif endloop endloop endloop One could improve on this further and further... Perhaps this helps you. Cheers Leon Am 08.06.2011 18:17, schrieb Leandro Zipitria:
|
