

先週末よりPythonを始めたばかりの初心者です。ネットデータベースを取り込み、定点的に分析するツールを作りたく、調べながら、その雛形を作成中です。  
詳しい人でしたら、すぐに原因がわかる内容かもしれませんが、お助けくださると感謝します。

## これまでに試したこと

コードを実行しますと、以下のエラーメッセージが出ます。

[code]

    ValueError                                Traceback (most recent call last)
    <ipython-input-179-6e43b379bc6b> in <module>
          6 exog_vars = ['expersq', 'union', 'married', 'year']
          7 #被説明変数には data.lwage を指定する
    ----> 8 mod = PooledOLS(data.lwage, sm.add_constant(data[exog_vars]))
          9 pooled_res = mod.fit()
         10 print(pooled_res)
    
    ~/opt/anaconda3/lib/python3.7/site-packages/linearmodels/panel/model.py in __init__(self, dependent, exog, weights)
        776         weights: Optional[PanelDataLike] = None,
        777     ) -> None:
    --> 778         super().__init__(dependent, exog, weights=weights)
        779 
        780     @classmethod
    
    ~/opt/anaconda3/lib/python3.7/site-packages/linearmodels/panel/model.py in __init__(self, dependent, exog, weights)
        228         weights: Optional[PanelDataLike] = None,
        229     ) -> None:
    --> 230         self.dependent = PanelData(dependent, "Dep")
        231         self.exog = PanelData(exog, "Exog")
        232         self._original_shape = self.dependent.shape
    
    ~/opt/anaconda3/lib/python3.7/site-packages/linearmodels/panel/data.py in __init__(self, x, var_name, convert_dummies, drop_first, copy)
        200             x = DataFrame(x)
        201         elif isinstance(x, Series):
    --> 202             raise ValueError("Series can only be used with a 2-level MultiIndex")
        203 
        204         if isinstance(x, DataFrame):
    
    ValueError: Series can only be used with a 2-level MultiIndex
    
[/code]

## 現状のコード

次の通りです。

[code]

    from linearmodels import PooledOLS
    import statsmodels.api as sm
    exog_vars = ['expersq', 'union', 'married', 'year']
    mod = PooledOLS(data.lwage, sm.add_constant(data[exog_vars]))
    pooled_res = mod.fit()
    print(pooled_res)
    
[/code]


CC BY-SA 4.0
2021-02-17T01:21:46.560
74064
2021-02-17T06:10:17.433
2021-02-17T01:24:42.777
32986
43962
question
python python3 jupyter-notebook
ValueError: Series can only be used with a 2-level MultiIndexを解決したいです


何か環境・版数・作業手順に関する問題と思われます。  
あるいは質問時の以前に何か別の作業を行っていて、そのままjupyterを再起動せず使い続け、その影響で状態が変わってエラーになったとかが考えられます。

以下の環境で：  
Windows10 20H2  
Anaconda3 Individual 2020.11  
Anaconda環境はconda update condaで最新化  
Python 3.8.6  
Pandas 1.2.2

lineamodels 4.18 conda install

以下の記事より：  
[Examples - linearmodels 4.19
(+12)](https://bashtage.github.io/linearmodels/devel/panel/examples/examples.html)

[1]のソースをコピー・実行後：

>
[code]

>     from linearmodels.datasets import wage_panel
>     import pandas as pd
>  
>     data = wage_panel.load()
>     year = pd.Categorical(data.year)
>     data = data.set_index(["nr", "year"])
>     data["year"] = year
>     print(wage_panel.DESCR)
>     print(data.head())
>  
[/code]

質問のソースをコピー・実行して：

[code]

    from linearmodels import PooledOLS
    import statsmodels.api as sm
    exog_vars = ['expersq', 'union', 'married', 'year']
    mod = PooledOLS(data.lwage, sm.add_constant(data[exog_vars]))
    pooled_res = mod.fit()
    print(pooled_res)
    
[/code]

正常に処理が行われた結果が表示されます。

[code]

                              PooledOLS Estimation Summary                          
    ================================================================================
    Dep. Variable:                  lwage   R-squared:                        0.1246
    Estimator:                  PooledOLS   R-squared (Between):              0.0902
    No. Observations:                4360   R-squared (Within):               0.1646
    Date:                Wed, Feb 17 2021   R-squared (Overall):              0.1246
    Time:                        14:46:24   Log-likelihood                   -3149.2
    Cov. Estimator:            Unadjusted                                           
                                            F-statistic:                      61.920
    Entities:                         545   P-value                           0.0000
    Avg Obs:                       8.0000   Distribution:                 F(10,4349)
    Min Obs:                       8.0000                                           
    Max Obs:                       8.0000   F-statistic (robust):             61.920
                                            P-value                           0.0000
    Time periods:                       8   Distribution:                 F(10,4349)
    Avg Obs:                       545.00                                           
    Min Obs:                       545.00                                           
    Max Obs:                       545.00                                           
                                                                                    
                                 Parameter Estimates                              
    ==============================================================================
                Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI
    ------------------------------------------------------------------------------
    const          1.3454     0.0222     60.606     0.0000      1.3019      1.3889
    expersq       -0.0021     0.0003    -7.5081     0.0000     -0.0026     -0.0015
    union          0.1768     0.0176     10.032     0.0000      0.1423      0.2114
    married        0.1521     0.0159     9.5417     0.0000      0.1209      0.1834
    year.1981      0.1187     0.0303     3.9144     0.0001      0.0592      0.1781
    year.1982      0.1843     0.0306     6.0168     0.0000      0.1243      0.2444
    year.1983      0.2431     0.0313     7.7581     0.0000      0.1817      0.3046
    year.1984      0.3322     0.0324     10.236     0.0000      0.2685      0.3958
    year.1985      0.4112     0.0341     12.048     0.0000      0.3443      0.4781
    year.1986      0.5039     0.0365     13.806     0.0000      0.4323      0.5754
    year.1987      0.5952     0.0396     15.026     0.0000      0.5176      0.6729
    ==============================================================================
    
[/code]


CC BY-SA 4.0
2021-02-17T06:10:17.433
74066
2021-02-17T06:10:17.433
26370
74064
answer