Upgrade to Pro — share decks privately, control downloads, hide ads and more …

effectsパッケージを用いた一般化線形モデルの可視化 /NagoyaR15

95d5cfc0ce395d0bfedeeb92d34261ce?s=47 Yu Tamura
March 28, 2016

effectsパッケージを用いた一般化線形モデルの可視化 /NagoyaR15

ロジスティック回帰や多項ロジスティック回帰などの一般化線形モデルを用いた分析の結果を図示するためのeffectsパッケージの紹介をします。

95d5cfc0ce395d0bfedeeb92d34261ce?s=128

Yu Tamura

March 28, 2016
Tweet

More Decks by Yu Tamura

Other Decks in Research

Transcript

  1. effectsύοέʔδΛ༻͍ͨ ҰൠԽઢܗϞσϧͷՄࢹԽ 2016.03.27 Nagoya.R #15 ໊ݹ԰େֶେֶӃࠃࡍ։ൃݚڀՊ D2 ాଜ༞ 1

  2. ֓ཁ • ର৅ • ઢܗϞσϧɼҰൠԽઢܗϞσϧΛ࢖ͬͨ෼ੳΛ ͢Δ͜ͱ͕͋Δํ • ಺༰ • ্هͷΑ͏ͳ෼ੳͷ݁ՌͷՄࢹԽΛศརʹ΍ͬ

    ͯ͘ΕΔeffectsύοέʔδͷ঺հ 2
  3. ීஈ͸͠ͳ͍ࣗݾ঺հ • ໊લ • ాଜ༞ • ॴଐ • ໊ݹ԰େֶେֶӃࠃࡍ։ൃݚڀՊത࢜ޙظ՝ఔ •

    ݚڀ෼໺ • ୈೋݴޠशಘɼ৺ཧݴޠֶɼจ๏ࢦಋ • Rྺ • ҰੜΤϯυϢʔβʔʢ3೥͘Β͍ʣ 3
  4. ͓அΓ • ࠓճ͸ɼͱ͘ʹࣗ෼ʹؔ܎ͷ͋ΔݚڀͷσʔλΛ ࢖͍·ͤΜʢ४උෆ଍ŗŖŕʣ • effectsύοέʔδʹೖ͍ͬͯΔαϯϓϧͷσʔλ ͷத͔Β2ͭΛ࢖͍·͢ 4

  5. ҰݴͰݴ͏ͱ • ෼ੳΛͨ͠ΒͱΓ͋͑ͣ • plot(alleffects(model)) • ͱ΍ͬͯΈ·͠ΐ͏ 5

  6. αϯϓϧσʔλᶃ • Cowles • Cowles, M., & Davis, C. (1987).

    The subject matter of psychology: Volunteers. British Journal of Social Psychology, 26, 97–102. • 4ม਺ • neuroticismʢ৘ॹෆ҆ఆੑʣ • extraversionʢ֎޲ੑʣ • sexʢੑผʣ • volunteerʢࠓޙͷௐࠪʹࣗओతʹࢀՃ͢Δ͔ʣ • Yes or Noͷ2୒ 6 ΞΠθϯΫੑ֨ ݕࠪͷείΞ
  7. த਎ΛݟͯΈΔ 7

  8. ෼ੳͯ͠ΈΔ • ੑผɼ֎޲ੑɼ৘ॹෆ҆ఆੑ͕ɼௐࠪ΁ͷࣗओ తࢀՃʹͲͷΑ͏ʹӨڹ͢Δͷ͔Λௐ΂͍ͨ 8 ैଐม਺ ಠཱม਺

  9. ෼ੳͯ͠ΈΔ • 2஋ม਺Λैଐม਺ͱͨ͠ϩδεςΟοΫճؼ෼ ੳ • ͱΓ͋͑ͣɼ3ͭͷಠཱม਺͢΂ͯͷओޮՌͱަ ޓ࡞༻ΛϞσϧʹ૊ΈࠐΉ 9

  10. 10 ΞελϦεΫͰͭͳ͙ͱओޮՌˍަޓ࡞༻ binomial͸ೋ߲෼෍ͷ͜ͱ ŗƀŕ

  11. ෼ੳͯ͠ΈΔ • 3࣍ͷަޓ࡞༻͸ͳ͛͞ͳͷͰɼAICͰϞσϧબ ୒ • glmؔ਺Λ࢖ͬͨϞσϦϯάͷ৔߹͸stepAIC͕ ࢖͑ΔͷͰศར 11

  12. 12

  13. ෼ੳͯ͠ΈΔ • Θ͔ͬͨ͜ͱ • ੑผͷओޮՌ͋Γ • ৘ॹෆ҆ఆੑͱ֎޲ੑͷަޓ࡞༻͋Γ ※ͪͳΈʹࢀՃऀIDΛϥϯμϜ੾ยʹͨ͠GLMM΋΍ͬͯΈ·͕ͨ͠ਪఆ ͕͏·͘Ͱ͖·ͤΜͰͨ͠ 13

  14. ͱݴΘΕͯ΋ ϐϯͱ͜ͳ͍ͷͰ 14

  15. ਤࣔͯ͠ΈΔ >eff.cowles <-allEffects(model2, xlevels =list(extraversion = seq(0,24,6)),given.values=c(sexmale = 0.5)) 15

    ͜ͷதʹGLMͷग़ ྗ͕ೖ͍ͬͯΔ xlevelsͰx࣠ͷ໨੝Γ Λࢦఆ ۠੾Γͷࢦఆɻʮ0͔Β 24·ͰΛ6ͣͭʯҙຯ ͜͏͢Δ͜ͱͰɼϞσϧ ͷத͔Βਤࣔʹඞཁͱͳ ΔΦϒδΣΫτΛ࡞Δ
  16. ਤࣔͯ͠ΈΔ • த਎͕Ͳ͏ͳ͍ͬͯΔͷ͔ݟͯΈΔͱ 16

  17. ਤࣔͯ͠ΈΔ • ͋ͱ͸ඳ͚ͩ͘ >plot(eff.cowles, ylab = “Prob(Volunteer)”) 17

  18. 18

  19. 19 extraversion = 0 extraversion = 6 extraversion = 12

    extraversion = 18 extraversion = 24
  20. ิ଍ • allEffects()͸ɼϞσϧͷதͷશͯͷ߲ΛऔΓग़͢ ৔߹ʹ࢖͏ • ϞσϧͷதͷҰ෦ͷΈͰྑ͍৔߹͸ɼeffect·ͨ ͸EffectΛ࢖ͬͯҎԼͷΑ͏ʹͰ͖Δ >eff.ne <-effect(“neuroticism*extraversion”,model2) >Eff.ne

    <-Effect(c(“neuroticism”, “extraversion”), model2) 20
  21. ิ଍ • multiline = TRUEͷࢦఆΛ͢Ε͹̍ͭͷਤʹ >plot(eff.cowles,”sex”,ylab=“Prob(Volunteer)”) 21

  22. ิ଍ • ਤΛݸผʹඳ͖͍ͨ৔߹͸߲Λࢦఆ͢Ε͹OK >plot(eff.cowles,”neuroticism:extraversion”,m ultiline = T,ylab=“Prob(Volunteer)”) • Ұ౓ม਺ʹೖΕͣʹplot಺ͰೖΕࢠͷܗʹ͢Ε͹ ಉ͡ਤʹͳΔ

    >plot(effect(“neuroticism:extraversion”,model 2,xlevels = seq(0,24,6))),multiline =T) 22
  23. 23

  24. αϯϓϧσʔλᶄ • BEPSʢBritish Election Panel Study) • effectsύοέʔδʹೖ͍ͬͯΔαϯϓϧσʔλ • 1997-2001೥ͷબڍʹؔ͢ΔΠΪϦεͷௐࠪ

    σʔλ 24
  25. • 10ม਺ • vote • Ͳͷ੓ౘʹ౤ථ͔ͨ͠ • Conservative, Labour, Liberal

    Democratͷ3ͭ • age • ೥ྸ • economi.cond.national • ݱࡏͷࠃͷܦࡁঢ়گʢ1-5Ͱධఆʣ • economic.cond.household • ݱࡏͷՈܭͷܦࡁঢ়گʢ1-5Ͱධఆʣ • Blair • ࿑ಇౘౘटBlairͷධՁʢ1-5Ͱධఆʣ • Hague • อकౘౘटHagueͷධՁʢ1-5Ͱධఆʣ • Kennedy • ࣗ༝ຽओౘౘटKennedyͷධՁʢ1-5Ͱධఆʣ • Europe • Ԥभջٙओٛʹର͢Δଶ౓ʢ1-11Ͱධఆʣ • ਺஋͕ߴ͍΄ͲԤभջٙओٛత • political.knowledge • ͦΕͧΕͷౘ͕Ԥभջٙओٛʹରͯ͠Ͳ͏͍ͬͨϙδγϣϯΛऔ͍ͬͯΔ͔ʹؔ͢Δ஌ࣝʢ0-3 Ͱධఆʣ • gender • male͔female͔ͷ2ਫ४ 25
  26. த਎ΛݟͯΈΔ 26 ͪͬ͞

  27. ෼ੳͯ͠ΈΔ • ͞·͟·ͳม਺͕౤ථߦಈʹͲͷΑ͏ͳӨڹΛ ༩͍͑ͯΔͷ͔ 27

  28. • 10ม਺ • vote • Ͳͷ੓ౘʹ౤ථ͔ͨ͠ • Conservative, Labour, Liberal

    Democratͷ3ͭ • age • ೥ྸ • economi.cond.national • ݱࡏͷࠃͷܦࡁঢ়گʢ1-5Ͱධఆʣ • economic.cond.household • ݱࡏͷՈܭͷܦࡁঢ়گʢ1-5Ͱධఆʣ • Blair • ࿑ಇౘౘटBlairͷධՁʢ1-5Ͱධఆʣ • Hague • อकౘౘटHagueͷධՁʢ1-5Ͱධఆʣ • Kennedy • ࣗ༝ຽओౘౘटKennedyͷධՁʢ1-5Ͱධఆʣ • Europe • Ԥभջٙओٛʹର͢Δଶ౓ʢ1-11Ͱධఆʣ • ਺஋͕ߴ͍΄ͲԤभջٙओٛత • political.knowledge • ͦΕͧΕͷౘ͕Ԥभջٙओٛʹରͯ͠Ͳ͏͍ͬͨϙδγϣϯΛऔ͍ͬͯΔ͔ʹؔ͢Δ஌ࣝʢ0-3 Ͱධఆʣ • gender • male͔female͔ͷ2ਫ४ 28 ैଐม਺ ಠཱม਺
  29. ෼ੳͯ͠ΈΔ • ΧςΰϦΧϧม਺ʢڞ࿨ౘɾࣗຽౘɾ࿑ಇౘʣ Λैଐม਺ʹͨ͠ଟ߲ϩδεςΟοΫճؼ • nnetύοέʔδͷmultinomؔ਺Λ࢖༻ 29

  30. 30 ͪͬ͞

  31. ਤࣔͯ͠ΈΔ • ͱΓ͋͑ͣ̍ͭͣͭΈͯΈΔ >plot(effect("age",beps),ylab="vote(Probability ") 31 ࣗຽౘ ࿑ಇౘ อकౘ

  32. ਤࣔͯ͠ΈΔ • ࠃͷܦࡁঢ়گͷධՁͱ౤ථ཰ >plot(effect(“economic.cond.national”,beps),yl ab="vote(Probability") 32 ࣗຽౘ ࿑ಇౘ อकౘ

  33. ਤࣔͯ͠ΈΔ • Ոܭͷܦࡁঢ়گͱ౤ථ཰ >plot(effect(“economic.cond.household”,beps) ,ylab="vote(Probability") 33 ࣗຽౘ ࿑ಇౘ อकౘ

  34. ਤࣔͯ͠ΈΔ • ౘटͷධՁ͸Ͳ͏͔ >plot(effect(“Blair”,beps),ylab=“vote(Probabilit y") >plot(effect(“Hague”,beps),ylab=“vote(Probabil ity") >plot(effect(“Kennedy”,beps),ylab="vote(Proba bility") 34

  35. ਤࣔͯ͠ΈΔ • ౘटͷධՁ͸Ͳ͏͔ >plot(effect(“Blair”,beps),ylab=“vote(Probabilit y") >plot(effect(“Hague”,beps),ylab=“vote(Probabil ity") >plot(effect(“Kennedy”,beps),ylab="vote(Proba bility") 35

    Μʁ
  36. ਤࣔͯ͠ΈΔ • ౘटͷධՁ͸Ͳ͏͔ >plot(effect(“Blair”,beps),ylab=“vote(Probabilit y") >plot(effect(“Hague”,beps),ylab=“vote(Probabil ity") >plot(effect(“Kennedy”,beps),ylab="vote(Proba bility") 36

    Χοίด͡๨ΕŗŖŕ
  37. 37 Balir(࿑ಇౘ) Hague(อकౘ) Kennedy(ࣗຽౘ) ͜Εʹؾ͍ͮͨํ͸Rݕఆ4ڃͰ͢ʢӕ

  38. "OZXBZ

  39. 39 x࣠ political.knowledge௿ x࣠ political.knowledgeߴ ύωϧ Ԥभջٙओٛత౓߹͍௿ ύωϧ Ԥभջٙओٛత౓߹͍ߴ

  40. ิ଍ᶄ • plot͢Δͱ͖ͷࡉ͔͍Ҿ਺ࢦఆ • band.colors :৴པ۠ؒͷόϯυͷ৭ʢσϑΥͰ փ৭ʣ • band.transparency:όϯυͷಁ͚ಁ͚۩߹ •

    ci.style: “bands”, “bars”, “lines”Ͱબ΂Δʢͬ͞ ͖·Ͱͷ΍ͭ͸”bands”Ͱ͢ʣ 40
  41. 41 band.colors="red",band.transparency=0.3,ci.style = "lines"

  42. ͓ΘΓʹ • ਤΛࡉ͔͍͘͡ΔͷͳΒ΍͸Γ௿ਫ४࡞ਤؔ਺ ͳͲΛγίγίͱۦ࢖ͨ͠ΓɼggplotΛ࢖ͬͯඳ ͘΄͏͕ྑ͍ͷ͔΋ʢݟͨ໨ʹͩ͜ΘΔํʣ • ͨͩ͠ɼeffΦϒδΣΫτΛ࡞Δ࡞ۀ͚ͩ΍ͬͯ ͓͍ͯɼͦͷޙʹggplotͰਤࣔͱ͍͏ύλʔϯ΋ Ͱ͖Δʢࠓճ͸ׂѪʣ 42

  43. ࢀߟจݙ Cowles, M., & Davis, C. (1987). The subject matter

    of psychology: Volunteers. British Journal of Social Psychology, 26, 97–102. Fox, J. (2003) Effect displays in R for generalised linear models. Journal of Statistical Software, 8, 1–27, doi: 10.18637/jss.v008.i15 Fox, J., Weisverg, S., Friendly, M., & Hong, J (2016) effects [R package version 3.0-7] Retrieved from https://cran.r-project.org/ package=effects 43
  44. effectsύοέʔδΛ༻͍ͨ ҰൠԽઢܗϞσϧͷՄࢹԽ contact info ాଜ ༞ ໊ݹ԰େֶେֶӃੜ yutamura@nagoya-u.jp http://www.tamurayu.wordpress.com/ 44

    ෼ੳͨ͠ΒͱΓ͋͑ͣ >plot(allEffects(model1)) ͯ͠ΈΔ