前提・実現したいこと
500人に対して50項目のアンケートを行ったファイルにおいて各検査結果を目的変数としたlogistic回帰を実行したい。(python)
ここに質問の内容を詳しく書いてください。
500人に対して50項目のアンケートを行ったファイルがあります。男性の場合1、女性の場合0として目的変数yは性別
説明変数をアンケート結果として表記しました。アンケート結果ははいを1、いいえを0としています。男女別の傾向をみるためにlogistic回帰を行おうと考えました。
ファイルの形式はcsvで500行(各人のID)、50列(各アンケート結果)
アンケートをx_1~x_50とし、y~x_1,x_2,・・・x_50と、1つ1つの検査結果を説明変数としてdf=1のlogistic解析を
50個まとめてpythonで施行したいと考えています。
発生している問題・エラーメッセージ
for文でエラーが表示される
エラーメッセージ
NameError Traceback (most recent call last)
<ipython-input-79-d0bb2b48b9f5> in <module>()
10 test_result = pd.read_csv("sample.csv")
11 test_result
---> 12 for i in [x_1,x_2,x_3,x_4,x_5,x_6,x_7,x_8,x_9,x_10,x_11,x_12,x_13,x_14,x_15,x_16,x_17,x_18,x_19,x_20,x_21,x_22,x_23,x_24,x_25,x_26,x_27,x_28,x_29,x_30,x_31,x_32,x_33,x_34,x_35,x_36,x_37,x_38,x_39,x_40,x_41,x_42,x_43,x_44,x_45,x_46,x_47,x_48,x_49,x_50]:
13 mod_glm = smf.glm(formula = "y ~ i",
14 data = test_result,
x_1 is not defined
該当のソースコード
import numpy as np
import pandas as pd
import scipy as sp
from matplotlib import pyplot as plt
import seaborn as sns
import statsmodels.formula.api as smf
import statsmodels.api as sm
%matplotlib inline
test_result = pd.read_csv("sample.csv")
test_result
for i in[x_1,x_2,x_3,x_4,x_5,x_6,x_7,x_8,x_9,x_10,x_11,x_12,x_13,x_14,x_15,x_16,x_17,x_18,x_19,x_20,x_21,x_22,x_23,x_24,x_25,x_26,x_27,x_28,x_29,x_30,x_31,x_32,x_33,x_34,x_35,x_36,x_37,x_38,x_39,x_40,x_41,x_42,x_43,x_44,x_45,x_46,x_47,x_48,x_49,x_50]:
mod_glm = smf.glm(formula = "y ~ i",
data = test_result,
family=sm.families.Binomial()).fit()
python
試したこと
mod_glm = smf.glm(formula = "y ~ x_1",
data = test_result,
family=sm.families.Binomial()).fit()
とそれぞれ入力すると問題なく施行できる。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/06 02:38