Gaussian kernel density estimation with data from filePlot and label data from file with PGFPlotsTikzpicture: Data from FilePGFPlots: importing data from a txt fileTikZ data visualization fails to load data from fileTikz plot from multiple-column data fileTranslate plot data from file in tikzDraw 3D surface from external data filedensity wave with pgfplotsTikz with gaussian pdfPgfplots plotting data from a csv file
In Dutch history two people are referred to as "William III"; are there any more cases where this happens?
Why does string strummed with finger sound different from the one strummed with pick?
How would fantasy dwarves exist, realistically?
Good examples of "two is easy, three is hard" in computational sciences
How can I monitor the bulk API limit?
Driving a school bus in the USA
Why does the setUID bit work inconsistently?
Can more than one instance of Bend Luck be applied to the same roll by multiple Wild Magic sorcerers?
What do you call bracelets you wear around the legs?
Bookshelves: the intruder
Working hours and productivity expectations for game artists and programmers
Lock out of Oracle based on Windows username
Why do academics prefer Mac/Linux?
How to get all possible paths in 0/1 matrix better way?
Why does Taylor’s series “work”?
Who is frowning in the sentence "Daisy looked at Tom frowning"?
Is it possible to determine from only a photo of a cityscape whether it was taken close with wide angle or from a distance with zoom?
Divisor Rich and Poor Numbers
Combining two Lorentz boosts
FIFO data structure in pure C
Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?
Are there any symmetric cryptosystems based on computational complexity assumptions?
Why is choosing a suitable thermodynamic potential important?
Why is the S-duct intake on the Tu-154 uniquely oblong?
Gaussian kernel density estimation with data from file
Plot and label data from file with PGFPlotsTikzpicture: Data from FilePGFPlots: importing data from a txt fileTikZ data visualization fails to load data from fileTikz plot from multiple-column data fileTranslate plot data from file in tikzDraw 3D surface from external data filedensity wave with pgfplotsTikz with gaussian pdfPgfplots plotting data from a csv file
I am trying to draw a histogram next to a density function, both with data from a file. The histogram is already working:
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
beginfilecontentsexample.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents
begindocument
begintikzpicture
beginaxis[ybar, ymin=0]
addplot[fill=black,
hist=
bins=11
] table [y index=0] example.dat;
endaxis
endtikzpicture
enddocument
My question is with the density function.
I would like to draw it using this formular (kernel density estimation):

EDIT start
where:
n: Number of datapointssigma: Standard deviation (User input)x_i: Datapointix: Function input variable (f(x))
EDIT end
I can't just plot it using addplot... because f(x) depends on all datapoints x_i.
I was thinking about using something like that somewhere:
pgfplotstablereadexample.dattable
pgfplotstablegetrowsoftable
pgfmathsetmacroRpgfplotsretval-1
pgfplotsinvokeforeach0,...,R
pgfplotstablegetelem#10oftable
pgfmathsetmacro value pgfplotsretval
% sum up all e^0.5(value-x)/sigma somhow
But I couldn't find a way to define a variable where I can add values in every iteration.
Here is an image from Wikipedia on Kernel density estimation:
The blue curve on the right is kind of what I would like to draw.
What's the best way of achieving that?
tikz-pgf
add a comment |
I am trying to draw a histogram next to a density function, both with data from a file. The histogram is already working:
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
beginfilecontentsexample.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents
begindocument
begintikzpicture
beginaxis[ybar, ymin=0]
addplot[fill=black,
hist=
bins=11
] table [y index=0] example.dat;
endaxis
endtikzpicture
enddocument
My question is with the density function.
I would like to draw it using this formular (kernel density estimation):

EDIT start
where:
n: Number of datapointssigma: Standard deviation (User input)x_i: Datapointix: Function input variable (f(x))
EDIT end
I can't just plot it using addplot... because f(x) depends on all datapoints x_i.
I was thinking about using something like that somewhere:
pgfplotstablereadexample.dattable
pgfplotstablegetrowsoftable
pgfmathsetmacroRpgfplotsretval-1
pgfplotsinvokeforeach0,...,R
pgfplotstablegetelem#10oftable
pgfmathsetmacro value pgfplotsretval
% sum up all e^0.5(value-x)/sigma somhow
But I couldn't find a way to define a variable where I can add values in every iteration.
Here is an image from Wikipedia on Kernel density estimation:
The blue curve on the right is kind of what I would like to draw.
What's the best way of achieving that?
tikz-pgf
add a comment |
I am trying to draw a histogram next to a density function, both with data from a file. The histogram is already working:
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
beginfilecontentsexample.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents
begindocument
begintikzpicture
beginaxis[ybar, ymin=0]
addplot[fill=black,
hist=
bins=11
] table [y index=0] example.dat;
endaxis
endtikzpicture
enddocument
My question is with the density function.
I would like to draw it using this formular (kernel density estimation):

EDIT start
where:
n: Number of datapointssigma: Standard deviation (User input)x_i: Datapointix: Function input variable (f(x))
EDIT end
I can't just plot it using addplot... because f(x) depends on all datapoints x_i.
I was thinking about using something like that somewhere:
pgfplotstablereadexample.dattable
pgfplotstablegetrowsoftable
pgfmathsetmacroRpgfplotsretval-1
pgfplotsinvokeforeach0,...,R
pgfplotstablegetelem#10oftable
pgfmathsetmacro value pgfplotsretval
% sum up all e^0.5(value-x)/sigma somhow
But I couldn't find a way to define a variable where I can add values in every iteration.
Here is an image from Wikipedia on Kernel density estimation:
The blue curve on the right is kind of what I would like to draw.
What's the best way of achieving that?
tikz-pgf
I am trying to draw a histogram next to a density function, both with data from a file. The histogram is already working:
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
beginfilecontentsexample.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents
begindocument
begintikzpicture
beginaxis[ybar, ymin=0]
addplot[fill=black,
hist=
bins=11
] table [y index=0] example.dat;
endaxis
endtikzpicture
enddocument
My question is with the density function.
I would like to draw it using this formular (kernel density estimation):

EDIT start
where:
n: Number of datapointssigma: Standard deviation (User input)x_i: Datapointix: Function input variable (f(x))
EDIT end
I can't just plot it using addplot... because f(x) depends on all datapoints x_i.
I was thinking about using something like that somewhere:
pgfplotstablereadexample.dattable
pgfplotstablegetrowsoftable
pgfmathsetmacroRpgfplotsretval-1
pgfplotsinvokeforeach0,...,R
pgfplotstablegetelem#10oftable
pgfmathsetmacro value pgfplotsretval
% sum up all e^0.5(value-x)/sigma somhow
But I couldn't find a way to define a variable where I can add values in every iteration.
Here is an image from Wikipedia on Kernel density estimation:
The blue curve on the right is kind of what I would like to draw.
What's the best way of achieving that?
tikz-pgf
tikz-pgf
edited 1 hour ago
Nathanael
asked 7 hours ago
NathanaelNathanael
808
808
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can sum these things up as follow. I use pgfplotsforeachungrouped in order to avoid making the variables global. I used some prescription to generate something. If you make your prescription more explicit, this can be adjusted accordingly.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
edefmysummysum+2*exp(-(x-pgfplotsretval-0.5)^2)
% sum up all e^0.5(value-x)/sigma somhow
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=40:100,thick,samples=501] mysum;
endaxis
endtikzpicture
enddocument

If you use
edefmysummysum+sqrt(2)*exp(-0.25*(x-pgfplotsretval-0.5)^2)
instead, you get

OLD ANSWER: I am not sure I got the normalization of the Gaussian right.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysummysum+pgfplotsretval
% sum up all e^0.5(value-x)/sigma somhow
pgfmathsetmacromyaveragemysum/R
pgfmathsetmacromysigma0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysigmamysigma+pow(pgfplotsretval-myaverage,2)
%typeoutmysum,myaverage,mysigma
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=0:100,thick,samples=101] sqrt(4*mysigma/(R*R))*exp(-R*(x-myaverage)^2/mysigma);
endaxis
endtikzpicture
enddocument

Thanks for your answer, but I think you misunderstood my question a bit. Maybe it wasn't clear enough. I want to draw a sum of gaussians (red in my last image) to get to the blue one. Sigma is a parameter I provide. But I think I can come up with a solution with the help of your answer!
– Nathanael
6 hours ago
1
@Nathanael I changed my answer. Do you have a concrete prescriptionor explanation for all the elements of your formula? Or is sigma just the standard variance?
– marmot
6 hours ago
Thanks. Looks good now. Will update my question when I get home in a couple of hours. Sigma is just the standard deviation of those gaussians. By changing it one can control the "smoothness" of the resulting curve.
– Nathanael
5 hours ago
add a comment |
documentclassarticle
beginfilecontentsexample.dat
71
54
.
.
.
95
81
endfilecontents
begindocument
<<echo=F,fig.cap="Histogram and density plot.">>=
data <- read.csv("example.dat", comment.char = "%",header=F)
hist(data$V1, freq=F, col="gray", main="", xlab="Example data")
lines(density(data$V1),col="blue",lwd=3)
@
enddocument

Of course, you can have some control over the density function, for instance with:
lines(density(data$V1,adjust=.5, bw=8),col="blue",lwd=3)
The result will be ...

add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f491164%2fgaussian-kernel-density-estimation-with-data-from-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can sum these things up as follow. I use pgfplotsforeachungrouped in order to avoid making the variables global. I used some prescription to generate something. If you make your prescription more explicit, this can be adjusted accordingly.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
edefmysummysum+2*exp(-(x-pgfplotsretval-0.5)^2)
% sum up all e^0.5(value-x)/sigma somhow
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=40:100,thick,samples=501] mysum;
endaxis
endtikzpicture
enddocument

If you use
edefmysummysum+sqrt(2)*exp(-0.25*(x-pgfplotsretval-0.5)^2)
instead, you get

OLD ANSWER: I am not sure I got the normalization of the Gaussian right.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysummysum+pgfplotsretval
% sum up all e^0.5(value-x)/sigma somhow
pgfmathsetmacromyaveragemysum/R
pgfmathsetmacromysigma0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysigmamysigma+pow(pgfplotsretval-myaverage,2)
%typeoutmysum,myaverage,mysigma
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=0:100,thick,samples=101] sqrt(4*mysigma/(R*R))*exp(-R*(x-myaverage)^2/mysigma);
endaxis
endtikzpicture
enddocument

Thanks for your answer, but I think you misunderstood my question a bit. Maybe it wasn't clear enough. I want to draw a sum of gaussians (red in my last image) to get to the blue one. Sigma is a parameter I provide. But I think I can come up with a solution with the help of your answer!
– Nathanael
6 hours ago
1
@Nathanael I changed my answer. Do you have a concrete prescriptionor explanation for all the elements of your formula? Or is sigma just the standard variance?
– marmot
6 hours ago
Thanks. Looks good now. Will update my question when I get home in a couple of hours. Sigma is just the standard deviation of those gaussians. By changing it one can control the "smoothness" of the resulting curve.
– Nathanael
5 hours ago
add a comment |
You can sum these things up as follow. I use pgfplotsforeachungrouped in order to avoid making the variables global. I used some prescription to generate something. If you make your prescription more explicit, this can be adjusted accordingly.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
edefmysummysum+2*exp(-(x-pgfplotsretval-0.5)^2)
% sum up all e^0.5(value-x)/sigma somhow
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=40:100,thick,samples=501] mysum;
endaxis
endtikzpicture
enddocument

If you use
edefmysummysum+sqrt(2)*exp(-0.25*(x-pgfplotsretval-0.5)^2)
instead, you get

OLD ANSWER: I am not sure I got the normalization of the Gaussian right.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysummysum+pgfplotsretval
% sum up all e^0.5(value-x)/sigma somhow
pgfmathsetmacromyaveragemysum/R
pgfmathsetmacromysigma0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysigmamysigma+pow(pgfplotsretval-myaverage,2)
%typeoutmysum,myaverage,mysigma
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=0:100,thick,samples=101] sqrt(4*mysigma/(R*R))*exp(-R*(x-myaverage)^2/mysigma);
endaxis
endtikzpicture
enddocument

Thanks for your answer, but I think you misunderstood my question a bit. Maybe it wasn't clear enough. I want to draw a sum of gaussians (red in my last image) to get to the blue one. Sigma is a parameter I provide. But I think I can come up with a solution with the help of your answer!
– Nathanael
6 hours ago
1
@Nathanael I changed my answer. Do you have a concrete prescriptionor explanation for all the elements of your formula? Or is sigma just the standard variance?
– marmot
6 hours ago
Thanks. Looks good now. Will update my question when I get home in a couple of hours. Sigma is just the standard deviation of those gaussians. By changing it one can control the "smoothness" of the resulting curve.
– Nathanael
5 hours ago
add a comment |
You can sum these things up as follow. I use pgfplotsforeachungrouped in order to avoid making the variables global. I used some prescription to generate something. If you make your prescription more explicit, this can be adjusted accordingly.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
edefmysummysum+2*exp(-(x-pgfplotsretval-0.5)^2)
% sum up all e^0.5(value-x)/sigma somhow
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=40:100,thick,samples=501] mysum;
endaxis
endtikzpicture
enddocument

If you use
edefmysummysum+sqrt(2)*exp(-0.25*(x-pgfplotsretval-0.5)^2)
instead, you get

OLD ANSWER: I am not sure I got the normalization of the Gaussian right.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysummysum+pgfplotsretval
% sum up all e^0.5(value-x)/sigma somhow
pgfmathsetmacromyaveragemysum/R
pgfmathsetmacromysigma0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysigmamysigma+pow(pgfplotsretval-myaverage,2)
%typeoutmysum,myaverage,mysigma
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=0:100,thick,samples=101] sqrt(4*mysigma/(R*R))*exp(-R*(x-myaverage)^2/mysigma);
endaxis
endtikzpicture
enddocument

You can sum these things up as follow. I use pgfplotsforeachungrouped in order to avoid making the variables global. I used some prescription to generate something. If you make your prescription more explicit, this can be adjusted accordingly.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
edefmysummysum+2*exp(-(x-pgfplotsretval-0.5)^2)
% sum up all e^0.5(value-x)/sigma somhow
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=40:100,thick,samples=501] mysum;
endaxis
endtikzpicture
enddocument

If you use
edefmysummysum+sqrt(2)*exp(-0.25*(x-pgfplotsretval-0.5)^2)
instead, you get

OLD ANSWER: I am not sure I got the normalization of the Gaussian right.
documentclass[tikz,border=3.14mm]standalone
usepackagepgfplots
pgfplotssetcompat=1.16
beginfilecontents*example.dat
71
54
55
54
98
76
93
95
86
88
68
68
50
61
79
79
73
57
56
57
97
80
91
94
85
88
45
58
78
81
74
60
57
58
95
81
endfilecontents*
begindocument
begintikzpicture
pgfplotstablereadexample.datdatatable
pgfplotstablegetrowsofdatatable
pgfmathsetmacroRpgfplotsretval-1
pgfmathsetmacromysum0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysummysum+pgfplotsretval
% sum up all e^0.5(value-x)/sigma somhow
pgfmathsetmacromyaveragemysum/R
pgfmathsetmacromysigma0
pgfplotsforeachungrouped X in 0,...,R
pgfplotstablegetelemX0ofdatatable
pgfmathsetmacromysigmamysigma+pow(pgfplotsretval-myaverage,2)
%typeoutmysum,myaverage,mysigma
beginaxis[ ymin=0]
addplot[ybar,fill=black,
hist=
bins=11
] table [y index=0] example.dat;
addplot[blue,domain=0:100,thick,samples=101] sqrt(4*mysigma/(R*R))*exp(-R*(x-myaverage)^2/mysigma);
endaxis
endtikzpicture
enddocument

edited 6 hours ago
answered 6 hours ago
marmotmarmot
127k6162308
127k6162308
Thanks for your answer, but I think you misunderstood my question a bit. Maybe it wasn't clear enough. I want to draw a sum of gaussians (red in my last image) to get to the blue one. Sigma is a parameter I provide. But I think I can come up with a solution with the help of your answer!
– Nathanael
6 hours ago
1
@Nathanael I changed my answer. Do you have a concrete prescriptionor explanation for all the elements of your formula? Or is sigma just the standard variance?
– marmot
6 hours ago
Thanks. Looks good now. Will update my question when I get home in a couple of hours. Sigma is just the standard deviation of those gaussians. By changing it one can control the "smoothness" of the resulting curve.
– Nathanael
5 hours ago
add a comment |
Thanks for your answer, but I think you misunderstood my question a bit. Maybe it wasn't clear enough. I want to draw a sum of gaussians (red in my last image) to get to the blue one. Sigma is a parameter I provide. But I think I can come up with a solution with the help of your answer!
– Nathanael
6 hours ago
1
@Nathanael I changed my answer. Do you have a concrete prescriptionor explanation for all the elements of your formula? Or is sigma just the standard variance?
– marmot
6 hours ago
Thanks. Looks good now. Will update my question when I get home in a couple of hours. Sigma is just the standard deviation of those gaussians. By changing it one can control the "smoothness" of the resulting curve.
– Nathanael
5 hours ago
Thanks for your answer, but I think you misunderstood my question a bit. Maybe it wasn't clear enough. I want to draw a sum of gaussians (red in my last image) to get to the blue one. Sigma is a parameter I provide. But I think I can come up with a solution with the help of your answer!
– Nathanael
6 hours ago
Thanks for your answer, but I think you misunderstood my question a bit. Maybe it wasn't clear enough. I want to draw a sum of gaussians (red in my last image) to get to the blue one. Sigma is a parameter I provide. But I think I can come up with a solution with the help of your answer!
– Nathanael
6 hours ago
1
1
@Nathanael I changed my answer. Do you have a concrete prescriptionor explanation for all the elements of your formula? Or is sigma just the standard variance?
– marmot
6 hours ago
@Nathanael I changed my answer. Do you have a concrete prescriptionor explanation for all the elements of your formula? Or is sigma just the standard variance?
– marmot
6 hours ago
Thanks. Looks good now. Will update my question when I get home in a couple of hours. Sigma is just the standard deviation of those gaussians. By changing it one can control the "smoothness" of the resulting curve.
– Nathanael
5 hours ago
Thanks. Looks good now. Will update my question when I get home in a couple of hours. Sigma is just the standard deviation of those gaussians. By changing it one can control the "smoothness" of the resulting curve.
– Nathanael
5 hours ago
add a comment |
documentclassarticle
beginfilecontentsexample.dat
71
54
.
.
.
95
81
endfilecontents
begindocument
<<echo=F,fig.cap="Histogram and density plot.">>=
data <- read.csv("example.dat", comment.char = "%",header=F)
hist(data$V1, freq=F, col="gray", main="", xlab="Example data")
lines(density(data$V1),col="blue",lwd=3)
@
enddocument

Of course, you can have some control over the density function, for instance with:
lines(density(data$V1,adjust=.5, bw=8),col="blue",lwd=3)
The result will be ...

add a comment |
documentclassarticle
beginfilecontentsexample.dat
71
54
.
.
.
95
81
endfilecontents
begindocument
<<echo=F,fig.cap="Histogram and density plot.">>=
data <- read.csv("example.dat", comment.char = "%",header=F)
hist(data$V1, freq=F, col="gray", main="", xlab="Example data")
lines(density(data$V1),col="blue",lwd=3)
@
enddocument

Of course, you can have some control over the density function, for instance with:
lines(density(data$V1,adjust=.5, bw=8),col="blue",lwd=3)
The result will be ...

add a comment |
documentclassarticle
beginfilecontentsexample.dat
71
54
.
.
.
95
81
endfilecontents
begindocument
<<echo=F,fig.cap="Histogram and density plot.">>=
data <- read.csv("example.dat", comment.char = "%",header=F)
hist(data$V1, freq=F, col="gray", main="", xlab="Example data")
lines(density(data$V1),col="blue",lwd=3)
@
enddocument

Of course, you can have some control over the density function, for instance with:
lines(density(data$V1,adjust=.5, bw=8),col="blue",lwd=3)
The result will be ...

documentclassarticle
beginfilecontentsexample.dat
71
54
.
.
.
95
81
endfilecontents
begindocument
<<echo=F,fig.cap="Histogram and density plot.">>=
data <- read.csv("example.dat", comment.char = "%",header=F)
hist(data$V1, freq=F, col="gray", main="", xlab="Example data")
lines(density(data$V1),col="blue",lwd=3)
@
enddocument

Of course, you can have some control over the density function, for instance with:
lines(density(data$V1,adjust=.5, bw=8),col="blue",lwd=3)
The result will be ...

edited 3 hours ago
answered 4 hours ago
FranFran
54.3k6122184
54.3k6122184
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f491164%2fgaussian-kernel-density-estimation-with-data-from-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown