![](/img/trans.png)
[英]Building an empirical cumulative distribution function and data interpolation in R
[英]Extracting/Exporting the Data of the Empirical Cumulative Distribution Function in R (ecdf)
我用R计算一些数据的ecdf。 我想在其他软件中使用结果。 我用R只是为了做“工作”,而不是为论文得出最终的图表。
# Plotting the a built in sampla data
plot(cars$speed)
# Assingning the data to a new variable name
myData = cars$speed
# Calculating the edcf
myResult = ecdf(myData)
myResult
# Plotting the ecdf
plot(myResult)
> # Plotting the a built in sampla data
> plot(cars$speed)
> # Assingning the data to a new variable name
> myData = cars$speed
> # Calculating the edcf
> myResult = ecdf(myData)
> myResult
Empirical CDF
Call: ecdf(myData)
x[1:19] = 4, 7, 8, ..., 24, 25
> # Plotting the ecdf
> plot(myResult)
> plot(cars$speed)
问题1
如何获得原始信息以便在其他软件(例如Excel,Matlab,LaTeX)中绘制ecdf图? 对于直方图功能,我可以写
res = hist(...)
我发现所有类似的信息
res$breaks
res$counts
res$density
res$mids
res$xname
问题2
如何计算逆ecdf? 假设我想知道有多少辆车的速度低于10 mph(示例数据是车速)。
多亏了user777的回答,我现在有了更多信息。 如果我用
> myResult(0:25)
[1] 0.00 0.00 0.00 0.00 0.04 0.04 0.04 0.08 0.10 0.12 0.18 0.22 0.30 0.38
[15] 0.46 0.52 0.56 0.62 0.70 0.76 0.86 0.86 0.88 0.90 0.98 1.00
我得到了0到25英里/小时的数据。 但是我不知道在哪里绘制数据点。 我确实想精确再现R图。
在这里,我每1英里每小时就有一个数据点。
在这里,我没有每1英里每小时的数据品脱。 如果有可用数据,我只有一个数据点。
# Plotting the a built in sample data
plot(cars$speed)
# Assingning the data to a new variable name
myData = cars$speed
# Calculating the edcf
myResult = ecdf(myData)
myResult
# Plotting the ecdf
plot(myResult)
# Have a look on the probability for 0 to 25 mph
myResult(0:25)
# Have a look on the probability but just where there ara data points
myResult(unique(myData))
# Saving teh stuff to a directory
write.csv(cbind(unique(myData), myResult(unique(myData))), file="D:/myResult.txt")
文件myResult.txt
看起来像
"","V1","V2"
"1",4,0.04
"2",7,0.08
"3",8,0.1
"4",9,0.12
"5",10,0.18
"6",11,0.22
"7",12,0.3
"8",13,0.38
"9",14,0.46
"10",15,0.52
"11",16,0.56
"12",17,0.62
"13",18,0.7
"14",19,0.76
"15",20,0.86
"16",22,0.88
"17",23,0.9
"18",24,0.98
"19",25,1
含义
注意:我有一个德语Excel,所以小数点符号是逗号而不是点。
ecdf
的输出是一个函数 ,以及其他对象类型。 您可以使用class(myResult)
进行验证, class(myResult)
显示对象myResult
的S4类。
如果输入myResult(unique(myData))
,则R将以出现在myData
中的所有不同值评估ecdf对象myResult
,并将其打印到控制台。 要保存输出,可以输入write.csv(cbind(unique(myData), myResult(unique(myData))), file="C:/Documents/My ecdf.csv")
将其保存到该文件路径。
ecdf不会告诉您有多少辆汽车在特定阈值以上/以下。 而是指出从数据集中随机选择的汽车高于或低于阈值的可能性 。 如果您对满足某些条件的汽车数量感兴趣,只需计算一下即可。 myData[myData<=10]
返回数据元素,而length(myData[myData<=10])
告诉您其中有多少个元素。
假设您的意思是您想知道从数据中随机选择的汽车低于10 mph的样本概率 ,这就是myResult(10)
给出的值。
如我所见,您的主要要求是在每个x
值处重现跳跃。 尝试这个:
> x <- c(cars$speed, cars$speed, 1, 28)
> y <- c((0:49)/50, (1:50)/50, 0, 1)
> ord <- order(x)
> plot(y[ord] ~ x[ord], type="l")
前50(x,y)对是跳跃的起点,后50对是终点,后两对是起点和终点值$(x_1-3,0)$和$(x_ {50} +3,1)$。 然后,您需要按$ x $的升序对值进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.