楼主: songtao07
11111 19

[问答] 哪位高手用R自己编写过广义矩估计的程序吗? [推广有奖]

  • 2关注
  • 5粉丝

已卖:2份资源

讲师

32%

还不是VIP/贵宾

-

威望
0
论坛币
37 个
通用积分
9.8413
学术水平
20 点
热心指数
30 点
信用等级
18 点
经验
2632 点
帖子
528
精华
0
在线时间
239 小时
注册时间
2010-6-15
最后登录
2025-12-5

楼主
songtao07 发表于 2011-10-26 22:24:53 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
哪位高手用R自己编写过广义矩估计的程序吗?
如果谁能提供,在下可以奉献100个论坛币!
最简单的就行如:yit=alpha*yi,t-1+yitai+uit,就行!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:广义矩估计 矩估计 Alpha 论坛币 最简单 程序

沙发
epoh 发表于 2011-10-28 19:10:23

简单模型,我看过matlab code

如果你熟悉matlab,我可上传.

model:Y=alpha+beta*X+eta

moment conditions:[E(eta);E(X*eta)]=0

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
zhangtao + 5 + 5 + 5 epoh老师,您要是方便,发上来我们学习学习.

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

藤椅
zhangtao 发表于 2011-10-28 20:37:47
epoh老师,您要是方便,发上来我们学习学习!非常感谢!
数学好就是要天天学

板凳
epoh 发表于 2011-10-28 21:28:50

原始文件来自:

    http://www.mathworks.com/matlabcentral/fileexchange/12114-gmm

为方便在matlab & R比较,我把数据截出.

gmmestimation.m
gmmweightmatrix.m
linearmodel01.m
gmm.xls
gmm.csv

   gmm_single_eq.rar (40.68 KB)


%%%%%%in matlab command window
data=xlsread('gmm.xls','a2:c1001');
X=data(:,1);
Y=data(:,2);
Z=[ones(length(X),1),data(:,3)];

number=100;
para0=[0;1];
[paraest,t_sta,V]=gmmestimation('linearmodel01',para0,Y,X,Z,number,2)

%paraest =
%    1.0144
%    1.9802
%t_sta =
%   87.3992
%  178.1722
%V =
%  1.0e-003 *
%    0.1347    0.0092
%    0.0092    0.1235
%%%%%%in R command window
library(gmm)
data=read.csv("gmm.csv")
y=data[,1]
z=data[,2]
iv=data[,3]
res=gmm(z~y,x=iv,type ="cue",vcov = "HAC")
summary(res)

Call:
gmm(g = z ~ y, x = iv, type = "cue", vcov = "HAC")
Method:  cue
Kernel:  Quadratic Spectral
Coefficients:
             Estimate    Std. Error  t value     Pr(>|t|)  
(Intercept)    1.014328    0.011195   90.605819    0.000000
y             1.980223    0.011052  179.166619    0.000000

J-Test: degrees of freedom is 0
                J-test                P-value            
Test E(g)=0:    7.80437246654343e-30  *******        

#############

#####in TSP

知道zhangtao兄对TSP也很有兴趣

也一并提供simplegmm.tsp的执行结果

simplegmm.tsp

simplegmm.rar (219 Bytes) 本附件包括:

  • simplegmm.tsp

gmm.xls for tsp

gmm.xls (100.5 KB)

         PROGRAM
COMMAND  ***************************************************************
1  options  memory=4;
2  FREQ N;
3  SMPL 1 1000;
4  READ(file='GMM.XLS');
5  frml eq1 y = alpha + beta*x;
6    param alpha beta;
7  GMM (INST=(C,Z)) EQ1 ;
8
8  END;
         EXECUTION
*******************************************************************************Current sample:  1 to 1000                         GENERALIZED METHOD OF MOMENTS
                         =============================
WITH STARTING VALUES VIA:
                       NONLINEAR TWO STAGE LEAST SQUARESEQUATIONS: EQ1INSTRUMENTS: C Z
.....
.....
Number of observations = 1000  E'PZ*E = .175761E-30                         Standard
Parameter  Estimate        Error       t-statistic   P-value
ALPHA      1.01433       .010882       93.2138       [.000]
BETA       1.98022       .011003       179.967       [.000]

已有 2 人评分学术水平 热心指数 信用等级 收起 理由
kk22boy + 5 + 5 + 5 热心帮助其他会员
zhangtao + 5 + 5 + 5 向epoh大师致以最真诚的感谢!

总评分: 学术水平 + 10  热心指数 + 10  信用等级 + 10   查看全部评分

报纸
zhangtao 发表于 2011-10-29 21:16:51
epoh 发表于 2011-10-28 21:28
原始文件来自:    http://www.mathworks.com/matlabcentral/fileexchange/12114-gmm为方便在matlab & R比较 ...
GVAR_Toolbox1.1_July2011.zip (24.54 MB)

epoh老师,您好!
    附件中的程序运行到这里提示:waiting for input
我尝试输入参数,但是总是提示错误,
epoh老师,您看看,应该如何输入参数呢?
非常感谢!
***************************************************************************
                                                                          
                               GVAR Toolbox 1.1                           
                                   July 2011                              
                                                                          
                                                                          
Alessandro Galesi, CEMFI Madrid & L.Vanessa Smith, University of Cambridge
***************************************************************************
>>> Type the interface filename (WITHOUT the .xls extension) that you would like to
    use and press enter. If you wish to use one of the demo interface files provided
    with the installation program, type gvarBriefDemo or gvarFullDemo, accordingly.
    Alternatively, type the name of your own interface file.
    Note that MatLab is case sensitive.

======================================================================================
Note:
1. After typing the interface filename, the program will start running and it will make
   a number of pauses, unless the "Running the program with pauses option" is disabled.
   (This is the case for the gvarBriefDemo, for which no pauses will be performed).
2. At each pause, you will be called upon to supply settings and/or check intermediate
   results in the MAIN worksheet of the interface file that will open automatically each
   time. Once this file has opened, always refer back to the MatLab command window for
   instructions and information.
3. Additional guidance is also available by clicking on many of the headings and field
   names within the MAIN worksheet.

-----------------------------------------------------------------------------------
  After every pause, once the required settings have been supplied and/or the inter-
  mediate results have been checked, you must save and close the interface file. In
  fact, it is recommended that you close excel completely, each time.
-----------------------------------------------------------------------------------

USING THE FULL DEMO INTERFACE FILE
If you are using the full demo interface file most of the required settings and
intermediate results are already provided. However, there are occassions where the
user is required to intervene. Once again, consult the MatLab command window for
instructions referring SPECIFICALLY to the use of the full demo interface file.
If no such instructions are given, just save and close the interface file whenever
it opens in order to proceed.
======================================================================================

wainting for input


数学好就是要天天学

地板
zhangtao 发表于 2011-10-29 21:26:44
[paraest,t_sta,V]=gmmestimation('linearmodel01',para0,Y,X,Z,number,2)
epoh老师,您好!
inearmodel01是一个函数,为什么在这里可以做为moment参数引用?

另外,我运行了三个程序后,我的感想是:
这三个GMM程序matlab和R的程序可以看到源文件,可以方便修改和学习,
但是TSP程序简洁,但是看不到源程序,不能修改,不方便。
您看我的看法正确吗?
数学好就是要天天学

7
zhangtao 发表于 2011-10-29 21:39:26
GVAR_for_Gauss_Dec10.zip (925.23 KB)
epoh老师,您好!
     附件中的gauss程序运行也有问题,您看看是什么问题?
非常感谢!
// Welcome to GAUSS
You've just run the GAUSS startup file.  It's called "startup" in your
GAUSS home directory.  You may modify it to contain your own custom
commands or simply delete it.
To run an example program, enter the following at the command prompt:
run ols.e
>>

*********************************************************************
Select 1 for fixed weights
Select 2 for time-varying weights
Note: Select 1 to replicate the results in DdPS(2007) and DHPS(2007).
Some results in DdPS(2007) are also provided for option 2.
**********************************************************************
? 2
**********************************************************************************
Select 0, for exact-identifying restrictions on the long run parameters when
          performing cointegration analysis of the VARX* models
Select 1, for over-identifying restrictions using the exchange rate expressed
          viz a viz the US dollar
Select 2, for over-identifying restrictions using the effective exchange rate
Notes:
(i) Select 0, to replicate the results in DdPS(2007)
(ii) Select 2, to replicate the results in Table 5 in DHPS(2007)
(iii) Options 1 and 2 should be used with the fixed weights option. Over-identifying
     restrictions are placed on 11 out of the 26 country/regions
***********************************************************************************
? 1
Options 1 and 2 should be used with the fixed weights option.
Please run the gvar program again selecting the fixed weights
option at the beginning of the program
Execution stopped in line 415 of C:\gauss6.0\gvar\gvarDecember2010
>>

数学好就是要天天学

8
zhangtao 发表于 2011-10-29 21:39:41
*********************************************************************
Select 1 for fixed weights
Select 2 for time-varying weights

Note: Select 1 to replicate the results in DdPS(2007) and DHPS(2007).
Some results in DdPS(2007) are also provided for option 2.
**********************************************************************

? 1

****************************************************************************************
The fixed weights are calculated based on the average of 3 (consecutive) years.
Enter the starting year.

Notes:
(i) Enter the year 1999 to replicate the results in both DdPS(2007) and DHPS(2007)
(ii) The latest starting year that can be selected for this dataset is: 2007
****************************************************************************************

? 1999

**********************************************************************************
Select 0, for exact-identifying restrictions on the long run parameters when
          performing cointegration analysis of the VARX* models
Select 1, for over-identifying restrictions using the exchange rate expressed
          viz a viz the US dollar
Select 2, for over-identifying restrictions using the effective exchange rate

Notes:
(i) Select 0, to replicate the results in DdPS(2007)
(ii) Select 2, to replicate the results in Table 5 in DHPS(2007)
(iii) Options 1 and 2 should be used with the fixed weights option. Over-identifying
     restrictions are placed on 11 out of the 26 country/regions
***********************************************************************************

? 1


****************************************
Individual country VARX order selection
****************************************
Select 1 for AIC
Select 2 for SBC

Note: Select 1 to replicate the results in DdPS(2007) and DHPS(2007)
? 1


***************************************************************************************
Select one of the following where p,q are the lags lengths of the domestic (endogenous)
and foreign (star) variables respectively:
1 for p=q
2 for p<=q
3 for p>q and q/=kmax
4 for no restriction on p,q

Note: Select 3 to replicate the results in both DdPS(2007) and DHPS(2007)
       kmax denotes the maximum order of the GVAR
****************************************************************************************
? 3


******************************************************************************
Individual country VARX order for domestic & star variables based on AIC
p: var order of domestic variables
q: var order of star variables
******************************************************************************

         Country                p                q
           China                1                1
        EuroArea                2                1
           Japan                2                1
       Argentina                2                1
          Brazil                2                1
           Chile                2                1
          Mexico                1                1
            Peru                2                1
       Australia                1                1
          Canada                1                1
      NewZealand                2                1
       Indonesia                2                1
           Korea                2                1
        Malaysia                1                1
     Philippines                2                1
       Singapore                1                1
        Thailand                2                1
           India                2                1
         SAfrica                2                1
     SaudiArabia                2                1
          Turkey                2                1
          Norway                2                1
          Sweden                2                1
     Switzerland                2                1
              UK                1                1
              US                2                1


*********************************************************************************
Select 1: To keep the var order specification for the endogenous variables
          the same as that given above
Select 2: To change the var order specification for the endogenous variables
          for a subset or all countries
Select 3: To set the var order of the endogenous variables equal for all countries

Note: Select 1 to replicate the results in DdPS(2007)
      Select 3 to replicate the results in DHPS(2007)
*********************************************************************************
? 1


***************************************************************************
Select 1: To keep the var order specification for the star variables
          the same as that given above
Select 2: To change the var order specification for the star variables
          for a subset or all countries
Select 3: To set the var order of the star variables equal for all countries

Note: Select 2 to replicate the results in DdPS(2007)
      Select 1 to replicate the results in DHPS(2007)
***************************************************************************

? 1

**************************************************************************************
Treatment of deterministics for cointegration analysis

Enter 4 for the case of unrestricted intercepts and restricted trends for all countries.
Enter 3 for the case of unrestricted intercepts and no trends for all countries.
Enter 0 to determine a mix of cases 3 and 4 for each country.
**************************************************************************************

? 3

************************************************************************
       Cointegration Tests for the Individual Country VARX* Models

Enter 0 for MacKinnon's asymptotic critical values.
Enter 1 for small sample simulated critical values.

Note: Select 0, to replicate the results in DdPS(2007) and DHPS(2007)
*************************************************************************
? 0


**************************************************************************
Select 5 or 10 percent critical values: enter 5 or 10

Note: Select 5 to replicate the results in both DdPS(2007) and DHPS(2007)
*************************************************************************
? 5

******************************************************
# coint relationships for the individual VARX models
******************************************************

         Country          # coint
           China                1
        EuroArea                2
           Japan                3
       Argentina                2
          Brazil                1
           Chile                1
          Mexico                2
            Peru                3
       Australia                4
          Canada                5
      NewZealand                3
       Indonesia                3
           Korea                4
        Malaysia                2
     Philippines                2
       Singapore                2
        Thailand                2
           India                1
         SAfrica                2
     SaudiArabia                2
          Turkey                1
          Norway                3
          Sweden                2
     Switzerland                3
              UK                2
              US                2


*******************************************************************************
Select 1: To keep the # cointegrating relations the same as that given above
Select 2: To change the # cointegrating relations for a subset or all countries
Select 3: To set the # cointegrating relations the same across all countries

Note: Select 1 to replicate the results in DdPS(2007)
      Select 2 to replicate the results in DHPS(2007)
********************************************************************************
? 1
The number of cointegrating relations are not the appropriate to replicate
existing results. Please try running the program again.
>>


C:\gauss6.0\gvar\gvarscDec10.src(223) : error G0087 : 'c:\gauss6.0\gvar\output\Info.out' : Couldn't open

C:\gauss6.0\gvar\gvarscDec10.src(4819) : error G0087 : 'c:\gauss6.0\gvar\output\VARX_SC.out' : Couldn't open

C:\gauss6.0\gvar\gvarscDec10.src(5364) : error G0087 : 'c:\gauss6.0\gvar\output\VARXord.out' : Couldn't open

数学好就是要天天学

9
epoh 发表于 2011-10-30 21:15:09

先回答6楼的问题

举个matlab简单的例子:

fminsearch

  Find minimum of unconstrained multivariable function

  using derivative-free method Equation

Syntax

  x = fminsearch(fun,x0)

%%%%%%example

banana = @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2;

[x,fval,exitflag] = fminsearch(banana,[-1.2, 1])

x =

    1.0000    1.0000

fval =

  8.1777e-010

exitflag =

     1

%%%%%%%%%%%

另在这个帖子

  https://bbs.pinggu.org/forum.php?mod=viewthread&tid=1203180&pid=10501719&page=2&extra=#pid10501719

我是用fmincon求解lstar系数

[para]=fmincon(@(para)lstarSSE(para,y,X,q),init,[],[],[],[],lb,ub)

10
epoh 发表于 2011-10-31 11:13:24

回覆 7,8楼 gauss code

由于是手动逐一输入参数,

比较繁复,我就点到为止.

*********************************************************************
Select 1 for fixed weights
Select 2 for time-varying weights

Note: Select 1 to replicate the results in DdPS(2007) and DHPS(2007).
Some results in DdPS(2007) are also provided for option 2.
**********************************************************************

? 1

****************************************************************************************
The fixed weights are calculated based on the average of 3 (consecutive) years.
Enter the starting year.

Notes:
(i) Enter the year 1999 to replicate the results in both DdPS(2007) and DHPS(2007)
(ii) The latest starting year that can be selected for this dataset is: 2007
****************************************************************************************

? 1999

**********************************************************************************
Select 0, for exact-identifying restrictions on the long run parameters when
          performing cointegration analysis of the VARX* models
Select 1, for over-identifying restrictions using the exchange rate expressed
          viz a viz the US dollar
Select 2, for over-identifying restrictions using the effective exchange rate

Notes:
(i) Select 0, to replicate the results in DdPS(2007)
(ii) Select 2, to replicate the results in Table 5 in DHPS(2007)
(iii) Options 1 and 2 should be used with the fixed weights option. Over-identifying
     restrictions are placed on 11 out of the 26 country/regions
***********************************************************************************

? 0
****************************************
Individual country VARX order selection
****************************************
Select 1 for AIC
Select 2 for SBC

Note: Select 1 to replicate the results in DdPS(2007) and DHPS(2007)
? 1
***************************************************************************************
Select one of the following where p,q are the lags lengths of the domestic (endogenous)
and foreign (star) variables respectively:
1 for p=q
2 for p<=q
3 for p>q and q/=kmax
4 for no restriction on p,q

Note: Select 3 to replicate the results in both DdPS(2007) and DHPS(2007)
       kmax denotes the maximum order of the GVAR
****************************************************************************************
? 3
******************************************************************************
Individual country VARX order for domestic & star variables based on AIC
p: var order of domestic variables
q: var order of star variables
******************************************************************************

         Country                p                q
           China                1                1
        EuroArea                2                1
           Japan                2                1
       Argentina                2                1
          Brazil                2                1
           Chile                2                1
          Mexico                1                1
            Peru                2                1
       Australia                1                1
          Canada                2                1
      NewZealand                2                1
       Indonesia                2                1
           Korea                2                1
        Malaysia                1                1
     Philippines                2                1
       Singapore                2                1
        Thailand                2                1
           India                2                1
         SAfrica                2                1
     SaudiArabia                2                1
          Turkey                2                1
          Norway                2                1
          Sweden                2                1
     Switzerland                2                1
              UK                1                1
              US                2                1

*********************************************************************************
Select 1: To keep the var order specification for the endogenous variables
          the same as that given above
Select 2: To change the var order specification for the endogenous variables
          for a subset or all countries
Select 3: To set the var order of the endogenous variables equal for all countries

Note: Select 1 to replicate the results in DdPS(2007)
      Select 3 to replicate the results in DHPS(2007)
*********************************************************************************
? 1

***************************************************************************
Select 1: To keep the var order specification for the star variables
          the same as that given above
Select 2: To change the var order specification for the star variables
          for a subset or all countries
Select 3: To set the var order of the star variables equal for all countries

Note: Select 2 to replicate the results in DdPS(2007)
      Select 1 to replicate the results in DHPS(2007)
***************************************************************************

? 1

**************************************************************************************
Treatment of deterministics for cointegration analysis

Enter 4 for the case of unrestricted intercepts and restricted trends for all countries.
Enter 3 for the case of unrestricted intercepts and no trends for all countries.
Enter 0 to determine a mix of cases 3 and 4 for each country.
**************************************************************************************

? 3

************************************************************************
       Cointegration Tests for the Individual Country VARX* Models

Enter 0 for MacKinnon's asymptotic critical values.
Enter 1 for small sample simulated critical values.

Note: Select 0, to replicate the results in DdPS(2007) and DHPS(2007)
*************************************************************************
? 0


**************************************************************************
Select 5 or 10 percent critical values: enter 5 or 10

Note: Select 5 to replicate the results in both DdPS(2007) and DHPS(2007)
*************************************************************************
? 5

******************************************************
# coint relationships for the individual VARX models
******************************************************

         Country          # coint
           China                1
        EuroArea                2
           Japan                3
       Argentina                2
          Brazil                1
           Chile                2
          Mexico                2
            Peru                3
       Australia                4
          Canada                4
      NewZealand                3
       Indonesia                3
           Korea                4
        Malaysia                2
     Philippines                2
       Singapore                3
        Thailand                2
           India                2
         SAfrica                2
     SaudiArabia                2
          Turkey                1
          Norway                3
          Sweden                2
     Switzerland                3
              UK                2
              US                2

*******************************************************************************
Select 1: To keep the # cointegrating relations the same as that given above
Select 2: To change the # cointegrating relations for a subset or all countries
Select 3: To set the # cointegrating relations the same across all countries

Note: Select 1 to replicate the results in DdPS(2007)
      Select 2 to replicate the results in DHPS(2007)
********************************************************************************
? 1

   .......
   gauss_procedure.txt (27.49 KB)

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-9 03:40