1

My question is replacing Gaussian distribution with chi square distribution in following codes. So it should show a chi-square distribution instead of a normal distribution. Thanks in advance.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{tikzpicture}
\begin{axis}[
 no markers, domain=0:8, samples=100,
 axis y line = none,
 axis x line* = bottom,
 every axis x label/.style={at=(current axis.right of origin),anchor=west},
 height=5cm, width=12cm,
 xtick={2.5,5.5}, xticklabels = {$$ , $$}, ytick=\empty,
 enlargelimits=false, clip=false, axis on top,
 grid = major
 ]
 \addplot [fill=cyan!20, draw=none, domain=0:2.5] {gauss(4,1)} \closedcycle;
\addplot [fill=cyan!20, draw=none, domain=5.5:8] {gauss(4,1)} \closedcycle;
\addplot [very thick,cyan!50!black] {gauss(4,1)};

\draw [yshift=-0.3cm, latex-latex](axis cs:4,0) -- node [fill=white] {$0.35$} 
(axis cs:4,0);

\draw [yshift=+2cm, latex-latex](axis cs:2.5,0) -- node [fill=white] {$H_0$ 
Do not Reject} (axis cs:5.5,0);

\draw [yshift=+2cm, latex-latex](axis cs:0,0) -- node [fill=white] {$H_0$ 
Reject} (axis cs:2.5,0);

\draw [yshift=+2cm, latex-latex](axis cs:5.5,0) -- node [fill=white] {$H_0$ 
Reject} (axis cs:8,0);
\end{axis}

\end{tikzpicture}
\end{document}
Bernard
  • 271,350
Mehmet OZC
  • 45
  • 7
  • No it is not a duplication. I cannot change gauss function in codes with a new function which can be called as chisquare. Because chi square has gamma function and I am not so good at functions in latex. any help will be appreciated – Mehmet OZC Jan 06 '18 at 13:15
  • I still think this is a duplicate. You seem to be right that the Gamma function cannot be easily written in LaTeX, that is why the first given solution in the aforementioned question shows a solution to this problem, when you have gnuplot installed, and the third solution shows an approximated solution using pure TikZ. – Stefan Pinnow Jan 06 '18 at 13:22
  • Stefan thanks for your comment. However I still need help. I even cannot change the loop in the aforementioned question to a single distribution plot. – Mehmet OZC Jan 06 '18 at 13:35
  • Then I recommend asking a follow-up questions. Include a link to the answer from where you "copied" the code from and state what you have tried so far. Then chances are high, that someone will help you. (Since your question now will be about pgfplots, please add this as a tag.) – Stefan Pinnow Jan 06 '18 at 13:50
  • @StefanPinnow Well this is a follow up what else is needed? – percusse Jan 06 '18 at 14:09
  • @MehmetOZC Can you state your question clearly such that the problem is precisely described including loops or math functions or colors or whatever you need to fix from that duplicate – percusse Jan 06 '18 at 14:10
  • First of all I have really copied the codes in my question many years ago from another code which is probably published in this platform and I am grateful to he\she\them. I am trying to clarify the problem. You can see a predefined function in the preamble of the code which is called as gauss and has two inputs with (\pgfmathdeclarefunction). I want to replace it with a new function which calculates chi square. – Mehmet OZC Jan 06 '18 at 14:15
  • Yes but what is it? What is the problem? Why is it not possible with the duplicate? This is not a math site so people don't know necessarily what you mean by that. – percusse Jan 06 '18 at 14:36
  • @percusse, for me it would be a follow-up question if OP would have included the "duplicate" question https://tex.stackexchange.com/q/40908/95441 and (in addition) would have stated that it is required to have a solution which uses \pgfmathdeclarefunction. In its current form I stick to it, that it is a duplicate. (Herbert's solution below by the way is (almost) exactly the same as his answer to the "duplicate" question...) – Stefan Pinnow Jan 06 '18 at 17:57
  • @StefanPinnow In any case 1 hour is in no way appropriate to close the question without consulting the OP and people can have difficulty formulating questions. But still, I agree with the rest of your remark. – percusse Jan 06 '18 at 18:03
  • @percusse, I agree that people can have difficulties on formulating questions, but I still think it is ok to start a close-vote. It is still possible to comment below the (closed) question and to edit it, which is then reviewed and can be opened again. So this is most likely opinion-based ... – Stefan Pinnow Jan 06 '18 at 18:17
  • No need to say but I'm new. I have asked a new question. https://tex.stackexchange.com/questions/409124/plotting-chi-square-hypothesis-testing-regions which shows a figure about the question. I planned to delete the question but I did not find any delete button. A moderator can close or even delete the question. It is not a problem for me. – Mehmet OZC Jan 06 '18 at 18:42
  • @MehmetOZC, there should be a delete button at the very end of the question, directly before the comments start. – Stefan Pinnow Jan 06 '18 at 18:51
  • I cannot delete it – Mehmet OZC Jan 06 '18 at 18:55
  • No, it's not possible to delete questions that have gotten upvoted answers. – Torbjørn T. Jan 06 '18 at 19:00

2 Answers2

4

Run with xelatex

\documentclass[pstricks]{standalone}
\usepackage{pst-func}  
\begin{document}

\psset{xunit=1.2cm,yunit=10cm,plotpoints=200}
\begin{pspicture*}(-0.75,-0.1)(10,0.65)
    \multido{\rnue=0.5+0.5,\iblue=0+10}{10}{%
        \psChiIIDist[linewidth=1pt,linecolor=blue!\iblue,nue=\rnue]{0.01}{9}}
    \psaxes[Dy=0.1]{->}(0,0)(9.5,.6)
\end{pspicture*}

\end{document}

enter image description here

  • Herbert thanks for your answer. It really produces chi square. It is in your previous post and I have work on it. However it is a pspicture. In my project I work with tikzpicture. Is it possible to write codes in tikzpicture instead of pspicture? Also I need a single distribution. In your code, it produces 10 distribution in the same figure. – Mehmet OZC Jan 06 '18 at 14:48
  • I always use PSTricks. Instead of \multido use only one \psChiIIDist. Then you'll get only one distribution –  Jan 06 '18 at 15:16
2

Not pretty code, but it works. The chi-square plotting is from cjorssen's answer to Plotting the chi square distribution with TikZ.

output of code

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[%
    restrict y to domain = 0:0.5,
    xtick=\empty,ytick=\empty,
    axis lines*=left,
    hide y axis,
    clip=false,
    width=8cm,
    height=4cm
]

   \addplot+[name path=chi,mark={}] gnuplot[raw gnuplot] {%
        isint(x) = (int(x)==x);
        log2 = 0.693147180559945;
        chisq(x,k)=k<=0||!isint(k)?1/0:x<=0?0.0:exp((0.5*k-1.0)*log(x)-0.5*x-lgamma(0.5*k)-k*0.5*log2);
        set xrange [1.00000e-5:15.0000];
        set yrange [0.00000:0.500000];
        samples=200;
        plot chisq(x,4)};

   \path [name path=xax] (\pgfkeysvalueof{/pgfplots/xmin}, \pgfkeysvalueof{/pgfplots/ymin}) 
                      -- (\pgfkeysvalueof{/pgfplots/xmax}, \pgfkeysvalueof{/pgfplots/ymin});

   \addplot [fill=blue!30] fill between[of=chi and xax, soft clip={domain=10:15}];

   \draw (10, \pgfkeysvalueof{/pgfplots/ymax}) -- (10, \pgfkeysvalueof{/pgfplots/ymin}-0.05)
         node[below,font=\footnotesize] {$\chi^2=2.547$};

   \draw [stealth-] (13.5, \pgfkeysvalueof{/pgfplots/ymin}) -- (13.5, \pgfkeysvalueof{/pgfplots/ymin}-0.1)
         node[below,font=\footnotesize] {Calculated $\chi^2=5.147$};

   \draw [stealth-, shorten >=2pt] (rel axis cs:0,0.5) -- node[fill=white,font=\scriptsize]{Not rejecting of $H_0$} (10,0 |- {rel axis cs:0,0.5});

   \draw [-stealth, shorten <=2pt] (10,0 |- {rel axis cs:0,0.5}) -- node[fill=white,align=center,font=\scriptsize]{Rejecting\\region\\of $H_0$} (rel axis cs:1,0.5);

  \end{axis}
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688