首先需要加载tips数据集,代码如下:

library(ggplot2)
data(tips)

设置种子数:

set.seed(123)

进行简单随机抽样,抽取50人:

sample_tips <- tips[sample(nrow(tips), 50, replace = TRUE), ]

接下来对抽样后的数据集进行描述统计分析。

  1. 性别(categorical variable)
# 统计各性别的人数
table(sample_tips$sex)

# 用条形图表示各性别人数
ggplot(sample_tips, aes(x = sex)) + 
  geom_bar() +
  labs(title = "Number of Males and Females in the Sample",
       x = "Gender", y = "Count")

可以看出,样本中男性和女性的数量基本相等。

  1. 吸烟状态(categorical variable)
# 统计各吸烟状态的人数
table(sample_tips$smoker)

# 用条形图表示各吸烟状态的人数
ggplot(sample_tips, aes(x = smoker)) + 
  geom_bar() +
  labs(title = "Number of Smokers and Non-Smokers in the Sample",
       x = "Smoking Status", y = "Count")

可以看出,样本中吸烟者和非吸烟者的数量基本相等。

  1. 就餐人数(categorical variable)
# 统计各就餐人数的人数
table(sample_tips$size)

# 用条形图表示各就餐人数的人数
ggplot(sample_tips, aes(x = size)) + 
  geom_bar() +
  labs(title = "Number of People in Each Party in the Sample",
       x = "Party Size", y = "Count")

可以看出,样本中2人和3人的就餐人数最多,4人和1人的就餐人数最少。

  1. 总消费额(numeric variable)
# 用直方图表示总消费额的分布
ggplot(sample_tips, aes(x = total_bill)) + 
  geom_histogram(binwidth = 2) +
  labs(title = "Distribution of Total Bill in the Sample",
       x = "Total Bill", y = "Count")

# 用箱线图表示总消费额的分布
ggplot(sample_tips, aes(y = total_bill)) + 
  geom_boxplot() +
  labs(title = "Distribution of Total Bill in the Sample",
       y = "Total Bill")

可以看出,总消费额的分布呈现右偏分布,存在极端值,中位数为20.84,平均数为21.68,标准差为9.11。

  1. 小费金额(numeric variable)
# 用直方图表示小费金额的分布
ggplot(sample_tips, aes(x = tip)) + 
  geom_histogram(binwidth = 0.5) +
  labs(title = "Distribution of Tip Amount in the Sample",
       x = "Tip Amount", y = "Count")

# 用箱线图表示小费金额的分布
ggplot(sample_tips, aes(y = tip)) + 
  geom_boxplot() +
  labs(title = "Distribution of Tip Amount in the Sample",
       y = "Tip Amount")

可以看出,小费金额的分布呈现右偏分布,存在极端值,中位数为2.5,平均数为2.98,标准差为1.63。

综上所述,通过对简单随机抽样后的tips数据集进行描述统计分析,可以得到以下结论:

  • 样本中男性和女性的数量基本相等。
  • 样本中吸烟者和非吸烟者的数量基本相等。
  • 样本中2人和3人的就餐人数最多,4人和1人的就餐人数最少。
  • 总消费额的分布呈现右偏分布,存在极端值,中位数为20.84,平均数为21.68,标准差为9.11。
  • 小费金额的分布呈现右偏分布,存在极端值,中位数为2.5,平均数为2.98,标准差为1.63。
使用R软件自带的数据 tips采用简单随机抽样进行有放回等概率抽样抽取 50 人种子数假设设置为 setseed123根据数据的不同类型选择合适的统计量进行简单的描述统计分析要包含箱线图、直方图和条形图如果是数值型变量要描述其分布形态同时对集中趋势和波动性程度的描述要考虑是否有极端值进行统计量的选择进行描述

原文地址: https://www.cveoy.top/t/topic/bF5V 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录