Python Linearmodels を利用したデータパネル分析に挑戦しています。
最後あと数行 というところでエラーが発生します
formula_fe関数の中身の書き方次第でエラーが変わるという謎の状態です。
参考にしてるページが
パネルデータ分析 Pythonで学ぶ入門計量経済学
こちらの中のLinearmodels を参考にしています。
元々のデータ内容がこの画像です。
元ファイルを確認しましたが空行などエラーにつながりそうなものは見つかりませんでした。
ネット上を探しても .fit()に関して同様のエラーが見当たりません
もしわかる人いらっしゃいましたらよろしくお願いします。
Python
1import numpy as np 2from scipy.stats import norm, gaussian_kde 3import matplotlib.pyplot as plt 4import pandas as pd 5import linearmodels 6from linearmodels.panel.data import PanelData 7from linearmodels.panel import PanelOLS, PooledOLS, RandomEffects, compare 8from collections import OrderedDict 9import wooldridge 10from statsmodels.formula.api import ols 11 12#csv読込 13csv_data = pd.read_csv(r'データパス', encoding="utf-8", low_memory=False) 14data_frame= pd.DataFrame(csv_data) 15 16#マルチインデックス化 17data=data_frame.set_index(['code','year'],drop=False) 18data.head() 19 20formula_fe = 'sales ~ netincome + roea + roeb + EntityEffects' 21 22mod_fe = PanelOLS.from_formula(formula_fe, data= data) 23result_fe = mod_fe.fit() <ここでエラー 24
エラーエラーパターン1
formula_fe = 'sales ~ netincome + roea + roeb + EntityEffects' の場合
ZeroDivisionError Traceback (most recent call last) <ipython-input-164-f37de24251bb> in <module> 1 mod_fe = PanelOLS.from_formula(formula_fe, data= data) ----> 2 result_fe = mod_fe.fit() D:\Users\yujin\anaconda3\lib\site-packages\linearmodels\panel\model.py in fit(self, use_lsdv, use_lsmr, low_memory, cov_type, debiased, auto_df, count_effects, **cov_config) 1889 weps_pooled = wy - wx @ _lstsq(wx, wy, rcond=None)[0] 1890 resid_ss_pooled = float(weps_pooled.T @ weps_pooled) -> 1891 num = (resid_ss_pooled - resid_ss) / df_num 1892 1893 denom = resid_ss / df_denom ZeroDivisionError: float division by zero
エラーパターン2
formula_fe = 'sales ~ netincome + roea + roeb + TimeEffects + EntityEffects' の場合
ValueError Traceback (most recent call last) <ipython-input-166-f37de24251bb> in <module> 1 mod_fe = PanelOLS.from_formula(formula_fe, data= data) ----> 2 result_fe = mod_fe.fit() D:\Users\yujin\anaconda3\lib\site-packages\linearmodels\panel\model.py in fit(self, use_lsdv, use_lsmr, low_memory, cov_type, debiased, auto_df, count_effects, **cov_config) 1754 ) 1755 if not weighted: -> 1756 y, x, ybar = self._fast_path(low_memory=low_memory) 1757 y_effects = np.array([0.0]) 1758 x_effects = np.zeros(x.shape[1]) D:\Users\yujin\anaconda3\lib\site-packages\linearmodels\panel\model.py in _fast_path(self, low_memory) 1527 x = cast(PanelData, x.general_demean(groups)) 1528 elif self.entity_effects and self.time_effects: -> 1529 y = cast(PanelData, y.demean("both", low_memory=low_memory)) 1530 x = cast(PanelData, x.demean("both", low_memory=low_memory)) 1531 elif self.entity_effects: D:\Users\yujin\anaconda3\lib\site-packages\linearmodels\panel\data.py in demean(self, group, weights, return_panel, low_memory) 535 if group == "both": 536 if not low_memory: --> 537 return self._demean_both(weights) 538 else: 539 return self._demean_both_low_mem(weights) D:\Users\yujin\anaconda3\lib\site-packages\linearmodels\panel\data.py in _demean_both(self, weights) 398 d = self.dummies(dummy, drop_first=True) 399 d.index = e.index --> 400 d = PanelData(d).demean(group, weights=weights) 401 d = d.values2d 402 e = e.values2d D:\Users\yujin\anaconda3\lib\site-packages\linearmodels\panel\data.py in __init__(self, x, var_name, convert_dummies, drop_first, copy) 238 raise TypeError("Only ndarrays, DataFrames or DataArrays are " "supported") 239 if convert_dummies: --> 240 self._frame = expand_categoricals(self._frame, drop_first) 241 self._frame = self._frame.astype(np.float64, copy=False) 242 D:\Users\yujin\anaconda3\lib\site-packages\linearmodels\panel\data.py in expand_categoricals(x, drop_first) 110 111 def expand_categoricals(x: DataFrame, drop_first: bool) -> DataFrame: --> 112 return concat( 113 [convert_columns(x[c], drop_first) for c in x.columns], axis=1, sort=False 114 ) D:\Users\yujin\anaconda3\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy) 272 ValueError: Indexes have overlapping values: ['a'] 273 """ --> 274 op = _Concatenator( 275 objs, 276 axis=axis, D:\Users\yujin\anaconda3\lib\site-packages\pandas\core\reshape\concat.py in __init__(self, objs, axis, join, keys, levels, names, ignore_index, verify_integrity, copy, sort) 329 330 if len(objs) == 0: --> 331 raise ValueError("No objects to concatenate") 332 333 if keys is None: ValueError: No objects to concatenate
あなたの回答
tips
プレビュー