#均值:总和/长度

mean()

#中位数:将数列排序,若个数为奇数,取排好序数列中间的值.若个数为偶数,取排好序数列中间两个数的平均值

median()

#R语言中没有众数函数

#分位数

quantile(data):列出0%,25%,50%,75%,100%位置处的数据

#可自己设置百分比

quantile(data,probs=0.975)

#方差:衡量数据集里面任意数值与均值的平均偏离程度

var()

#标准差:

sd()

#直方图,binwidth表示区间宽度为1

ggplot(heights.weights, aes(x = Height)) +geom_histogram(binwidth = 1)

#发现上图是对称的,使用直方图时记住:区间宽度是强加给数据的一个外部结构,但是它却同时揭示了数据的内部结构

#把宽度改成5

ggplot(heights.weights, aes(x = Height)) +geom_histogram(binwidth = 5)

#从上图看,对称性不存在了,这叫过平滑,相反的情况叫欠平滑,如下图

ggplot(heights.weights, aes(x = Height)) +geom_histogram(binwidth = 0.01)

#因此合适的直方图需要调整宽度值.可以选择其他方式进行可视化,即密度曲线图

ggplot(heights.weights, aes(x = Height)) +geom_density()

#如上图,峰值平坦,尝试按性别划分数据

ggplot(heights.weights, aes(x = Height, fill = Gender)) +geom_density()

#混合模型,由两个标准分布混合而形成的一个非标准分布

#正态分布,钟形曲线或高斯分布

#按性别分片

ggplot(heights.weights, aes(x = Weight, fill = Gender)) +geom_density() +facet_grid(Gender ~ .)

#以下代码指定分布的均值和方差,m和s可以调整,只是移动中心或伸缩宽度

m <- 0
s <- 1
ggplot(data.frame(X = rnorm(100000, m, s)), aes(x = X)) +geom_density()

#构建出了密度曲线,众数在钟形的峰值处

#正态分布的众数同时也是均值和中位数

#只有一个众数叫单峰,两个叫双峰,两个以上叫多峰

#从一个定性划分分布有对称(symmetric)分布和偏态(skewed)分布

#对称(symmetric)分布:众数左右两边形状一样,比如正态分布

#这说明观察到小于众数的数据和大于众数的数据可能性是一样的.

#偏态(skewed)分布:说明在众数右侧观察到极值的可能性要大于其左侧,称为伽玛分布

#从另一个定性区别划分两类数据:窄尾分布(thin-tailed)和重尾分布(heavy-tailed)

#窄尾分布(thin-tailed)所产生的值通常都在均值附近,可能性有99%

#柯西分布(Cauchy distribution)大约只有90%的值落在三个标准差内,距离均值越远,分布特点越不同

#正态分布几乎不可能产生出距离均值有6个标准差的值,柯西分布有5%的可能性

#产生正态分布及柯西分布随机数

set.seed(1)
normal.values <- rnorm(250, 0, 1)
cauchy.values <- rcauchy(250, 0, 1)
range(normal.values)
range(cauchy.values)

#画图

ggplot(data.frame(X = normal.values), aes(x = X)) +geom_density()


ggplot(data.frame(X = cauchy.values), aes(x = X)) +geom_density()

#正态分布:单峰,对称,钟形窄尾

#柯西分布:单峰,对称,钟形重尾

#产生gamma分布随机数

gamma.values <- rgamma(100000, 1, 0.001)

ggplot(data.frame(X = gamma.values), aes(x = X)) +geom_density()

#游戏数据很多都符合伽玛分布

#伽玛分布只有正值

#指数分布:数据集中频数最高是0,并且只有非负值出现

#例如,企业呼叫中心常发现两次收到呼叫请求的间隔时间看上去符合指数分布

#散点图

ggplot(heights.weights, aes(x = Height, y = Weight)) +geom_point()

#加平滑模式

ggplot(heights.weights, aes(x = Height, y = Weight)) +geom_point() +geom_smooth()

ggplot(heights.weights[1:20, ], aes(x = Height, y = Weight)) +geom_point() +geom_smooth()


ggplot(heights.weights[1:200, ], aes(x = Height, y = Weight)) +geom_point() +geom_smooth()


ggplot(heights.weights[1:2000, ], aes(x = Height, y = Weight)) +geom_point() +geom_smooth()

ggplot(heights.weights, aes(x = Height, y = Weight)) +
geom_point(aes(color = Gender, alpha = 0.25)) +
scale_alpha(guide = "none") +
scale_color_manual(values = c("Male" = "black", "Female" = "gray")) +
theme_bw()

# An alternative using bright colors.
ggplot(heights.weights, aes(x = Height, y = Weight, color = Gender)) +
geom_point()

#
# Snippet 35
#

heights.weights <- transform(heights.weights,
Male = ifelse(Gender == 'Male', 1, 0))

logit.model <- glm(Male ~ Weight + Height,
data = heights.weights,
family = binomial(link = 'logit'))

ggplot(heights.weights, aes(x = Height, y = Weight)) +
geom_point(aes(color = Gender, alpha = 0.25)) +
scale_alpha(guide = "none") +
scale_color_manual(values = c("Male" = "black", "Female" = "gray")) +
theme_bw() +
stat_abline(intercept = -coef(logit.model)[1] / coef(logit.model)[2],
slope = - coef(logit.model)[3] / coef(logit.model)[2],
geom = 'abline',
color = 'black')

最新文章

  1. webpack+react+es6开发模式
  2. js弹出框、对话框、提示框、弹窗总结
  3. 记录最初工作下的笔记($.each)
  4. python中subprocess.Popen.poll
  5. 浅谈reverse_iterator的base()函数
  6. Android padding和margin的区别
  7. Kubernetes集群部署史上最详细(一)Kubernetes集群安装
  8. 【最大公约数&amp;链表】权值 @upcexam5921
  9. Oracle 修改表 Alter Table...
  10. 694. Number of Distinct Islands 形状不同的岛屿数量
  11. Java多线程-----实现生产者消费者模式的几种方式
  12. C++ 的虚析构函数
  13. 转:Xcode打印堆栈信息
  14. 【LOJ】#2186. 「SDOI2015」道路修建
  15. pyHook监听用户鼠标、键盘事件
  16. 基础拾遗 C# Json 与对象相互转换
  17. HTTP访问的两种方式:HttpURLConnection和HTTPClient的比较
  18. shell 中各种符号的含义
  19. Redis 基本操作(一)
  20. 结构体sockadrr、sockaddr_in、in_addr的定义

热门文章

  1. 如何在Asp.net中备份Access数据库?
  2. 了解Javascript 变量
  3. vuforia 结合 unity3d 开发 AR 的 androidAPP 总结
  4. hadoop1.2.1配置文件
  5. 7 天玩转 ASP.NET MVC — 第 7 天
  6. poj 2311 Cutting Game 博弈论
  7. lintcode:买卖股票的最佳时机 III
  8. lintcode: 跳跃游戏 II
  9. ubuntu 12.04安装vncserver
  10. 计算机IT求职学习List