def random_effect( df , group , X_cols , Y_cols ): """ 変量効果モデル(※ではない。今後修正) """ fixed = _convert( df=df , group=group ) params = {} for key , value in fixed.items(): X = lib.df2mat( df=value , columns=X_cols ) Y = lib.df2mat( df=value , columns=Y_cols ) b = lib.reg( X=X , Y=Y ) params[key] = b return params
#!-*-coding:utf-8-*- import lib import sys import numpy as np """ 6 Inference2 """ lib.title("#############6 Inference2#############") #####データを読み取る##### HPRICE_dataset = lib.load(filename="HPRICE1.csv") explanatories = ["const", "sqrft", "bdrms"] explained = ["lprice"] #####各変数を定義##### X = lib.df2mat(df=HPRICE_dataset, columns=explanatories) Y = lib.df2mat(df=HPRICE_dataset, columns=explained) b = lib.reg(X=X, Y=Y) """ 6-1の解答 """ lib.chaper("<6.1の解答>") #####解答##### lib.add_suffix(b, labels=explanatories) print("\n") """ 6-2の解答 """ lib.chaper("<6.2の解答>") #####回帰係数の取得##### const, sqrft, bdrms = b[0], b[1], b[2] y_bdrms = bdrms
#!-*-coding:utf-8-*- import lib import sys import numpy as np """ 1 Matrix Calculus """ lib.title("#############1 Matrix Calculus#############") #####データを読み取る##### artifical_dataset = lib.load(filename="artificial.csv") explanatories = ["x1", "x2"] explained = ["y"] #####各変数を定義##### X = lib.df2mat(df=artifical_dataset, columns=explanatories) Y = lib.df2mat(df=artifical_dataset, columns=explained) b = lib.reg(X=X, Y=Y) """ 1-1の解答 """ lib.chaper("<大問1.1の回答>") #####解答##### lib.add_suffix(b) print("\n") """ 1-2の解答 """ lib.chaper("<大問1.2の回答>") #####各変数を定義#####
import lib import sys import numpy as np """ 3 Multiple Regression """ lib.title("#############3 Multiple Regression#############") #####データを読み取る##### CEOSAL2_dataset = lib.load(filename="CEOSAL2.csv") explanatories = ["const", "lsales", "lmktval"] explained = ["lsalary"] """ 3-1の解答 """ lib.chaper("大問3.1の回答") X = lib.df2mat(df=CEOSAL2_dataset, columns=explanatories) Y = lib.df2mat(df=CEOSAL2_dataset, columns=explained) b = lib.reg(X=X, Y=Y) lib.add_suffix(coefs=b, labels=explanatories) print("\n") """ 3-2の解答 """ lib.chaper("大問3.2の回答") CEOSAL2_dataset = lib.load(filename="CEOSAL2.csv") explanatories = ["const", "lsales", "lmktval", "profits"] explained = ["lsalary"] X = lib.df2mat(df=CEOSAL2_dataset, columns=explanatories) Y = lib.df2mat(df=CEOSAL2_dataset, columns=explained) b = lib.reg(X=X, Y=Y)
8 Instrumental Variable And 2SLS """ lib.title("#############8 Instrumental Variable And 2SLS#############") FERTIL2_dataset = lib.load( filename="FERTIL2.csv" ) FERTIL2_dataset = lib.cross_var( df=FERTIL2_dataset , var1="age" , var2="age" ) """ 8-1の解答 """ lib.chaper("<8.1の解答>") #####説明変数を定義##### explanatories = ["const","educ","age","age*age"] explained = ["children"] X = lib.df2mat( df=FERTIL2_dataset , columns=explanatories ) Y = lib.df2mat( df=FERTIL2_dataset , columns=explained ) b = lib.reg( X=X , Y=Y ) lib.add_suffix( coefs=b , labels=explanatories ) const , educ , age , age_aqure = b[0] , b[1] , b[2] , b[3] y = educ * 100 print( y ) print( "9人減る" ) print("\n") """ 8-2の解答 """ lib.chaper("<8.2の解答>")
#!-*-coding:utf-8-*- import lib import sys import numpy as np """ 4 Multiple Regression """ lib.title("#############4 Multiple Regression#############") LOANAPP_dataset = lib.load( filename="LOANAPP.csv" ) explanatories = ["const","white"] explained = ["approve"] #####各変数を定義##### X = lib.df2mat( df=LOANAPP_dataset , columns=explanatories ) Y = lib.df2mat( df=LOANAPP_dataset , columns=explained ) """ 4-1の解答 """ lib.chaper( "大問4.1の回答" ) print( "係数が正の状態で、統計的に有意な値をとる" ) print("\n") """ 4-2の解答 """ lib.chaper( "大問4.2の回答" ) b = lib.reg( X=X , Y=Y ) lib.add_suffix( b ) t = lib.t( X=X , Y=Y , beta=b )[0] print( "t値 :", t )
""" 5-2の解答 """ lib.chaper("<5.2の解答>") print( "β1 - β2 = 0" ) print("\n") """ 5-3の解答 """ lib.chaper("<5.3の解答>") #####説明変数を定義##### explanatories = ["const","lexpendA","lexpendB","prtystrA"] explained = ["voteA"] X = lib.df2mat( df=VOTE_dataset , columns=explanatories ) Y = lib.df2mat( df=VOTE_dataset , columns=explained ) b = lib.reg( X=X , Y=Y ) t_expendA = lib.t( X=X , Y=Y , beta=b )[1] lib.add_suffix( coefs=b , labels=explanatories ) print( "Aの支出は、正に、Bの支出は負に影響を与えている" ) print( "Aの支出は" + str( b[1] ) + "であり、t値が" +\ str( round( t_expendA , 3 ) ) + "であり1%有意であることから、結果に大きな影響を与えると言える" ) print( "利用はできるが、係数が逆なのでイコールとして採択はできない" ) print("\n") """
import numpy as np """ 7 Panel Data """ lib.title("#############7 Panel Data#############") grunfeld_dataset = lib.load(filename="grunfeld.csv") """ 7-1の解答 """ lib.chaper("<7.1の解答>") explanatories = ["const", "value", "capital"] explained = ["invest"] #B = lib.random_effect( df=grunfeld_dataset , group="year" , X_cols=explanatories , Y_cols=explained ) X = lib.df2mat(df=grunfeld_dataset, columns=explanatories) Y = lib.df2mat(df=grunfeld_dataset, columns=explained) b = lib.reg(X=X, Y=Y) print("提出の時は、βにちゃんと係数をいれましょう ") lib.add_suffix(coefs=b, labels=explanatories) print("\n") """ 7-2の解答 """ lib.not_done("<7.2の解答>") print("土屋くんに聞く") firms = list(set(list(grunfeld_dataset["firm"]))) for i in range(len(firms)): print(firms[i]) this = "hetero" + str(i)