《统计学习方法-基于R语言》第二章8-10题

实验1: R语言统计分析

1. 实验目的

通过实操来练习R语言统计分析的技能,并达到对R语言基本语法和基本绘图函数的熟悉和应用。同时对一些统计分析常用的函数(如:数据如何分布,中心趋势,离散度的计算,以及如何清洗数据,如何进行可视化分析)进行学习。掌握描述性统计方法以理解数据基本特征;运用推断性统计检验总体假设;进行功效分析以确定样本需求。

2. 实验准备

2.1 实验平台

windows11系统,R4.4.1。

2.2 数据简述

college数据集(ISLR包)
在这里插入图片描述
Auto数据集(ISLR包)
在这里插入图片描述
Boston数据集(MASS包)
在这里插入图片描述

2.3 软件配置

软件:R4.4.1;
包:ISLR,MASS
下载方式:

install.packages("ISLR")
install.packages("MASS")

3. 实验内容

实验内容为《统计学习导论:基于R应用》第二章习题第8-10题,依次为8,9,10题。

3.1 第八题

对College数据集中的777所美国院校进行数据分析:

(a)Use the read.csv() function to read the data into R. Call the loaded data college

setwd("C:\\Users\\pppp\\Desktop\\CB")
write.csv(College,file = "College.csv")
college = read.csv(file="College.csv",header = TRUE)

(b)Look at the data using the fix() function.

fix(college)
rownames (college)=college [,1]

此处是将college数据集中的第一列作为行名。
(c )
i.Use the summary() function to produce a numerical summary of the variables in the data set.

summary(college)

在这里插入图片描述
summary()函数用于对数据进行汇总计算,此处private是定性变量,不做统计,其余的变量皆为定量变量,分别用Min,1st qu ,median,mean,3rd qu,max来表示该列数据的最小值,第一四分位数,中位数,均值,第三四分位数,最大值。

ii. Use the pairs() function to produce a scatterplot matrix of the first ten columns or variables of the data.

Private=as.factor (college$Private) #设置为定性变量
college=data.frame(Private,college[,2:11])
pairs(college)

在这里插入图片描述
该图就展示了各变量之间的分布关系,如Apps与Accept呈正相关。

iii. Use the plot() function to produce side-by-side boxplots of Outstate versus Private.

plot(Private,college$Outstate,ylab="Outstate",xlab="Private")

在这里插入图片描述
从该图中,不难看出私立学校的Outstate显著高于公立学校。

iv. Use the summary() function to see how many elite universities there are.
Now use the plot() function to produce side-by-side boxplots of Outstate versus Elite.

Elite=rep("No",nrow(college )) #根据行数创建一个同样行数的向量
Elite[college$Top10perc >50]=" Yes" #按照要求修改值
Elite=as.factor(Elite) #转为因子(定性)
college=data.frame(college , Elite) #合并
summary(college)

在这里插入图片描述
78所精英大学

plot(Elite,college$Outstate,ylab="Outstate",xlab="Elite")

在这里插入图片描述
此图展示了精英大学的outstate显著高于非精英大学。

v. Use the hist() function to produce some histograms with differing numbers of bins for a few of the quantitative variables.

par(mfrow=c(3,3))
for(i in 3:18){  
  hist(college[,i],col =2, breaks =20,xlab=name[i])
}

在这里插入图片描述
此图展示了不同的参数在不同区间出现的频数。

3.2 第九题

对Auto数据集进行数据分析:
(a) Which of the predictors are quantitative, and which are qualitative?

names(Auto)
summary(Auto)

在这里插入图片描述
其中,name,origin是定性的, 其他都是定量的。
(b) What is the range of each quantitative predictor? You can answer this using the range() function.
(c ) What is the mean and standard deviation of each quantitative predictor?

len=matrix(0,8,4)
for(l in 1:8){
  len[l,]=range(Auto[,l])#变量取值范围
  len[l,3]=var(Auto[,l])#变量方差
  len[l,4]=mean((Auto[,l]))#变量均值
}

name=matrix(names(Auto[,1:8]),8,1)#提取变量名
len=cbind(name,len)#组合数据表
len=data.frame(len)
names(len)=c("变量名","最小值","最大值","方差","均值")
len

在这里插入图片描述
(d) Now remove the 10th through 85th observations. What is the range, mean, and standard deviation of each predictor in the subset of the data that remains?

Auto2=Auto[-c(10,85),]

len2=matrix(0,8,4)
for(l in 1:8){
  len2[l,]=range(Auto2[,l])
  len2[l,3]=sd(Auto2[,l])
  len2[l,4]=mean((Auto2[,l]))
}
name2=matrix(names(Auto[,1:8]),8,1)
len2=cbind(name2,len2)
len2=data.frame(len2)
names(len2)=c("变量名","最小值","最大值","标准差","均值")
len2

在这里插入图片描述
(e) Using the full data set, investigate the predictors graphically, using scatterplots or other tools of your choice. Create some plots highlighting the relationships among the predictors. Comment on your findings.

pairs(Auto[,1:8],main="Auto's matrix scatter plot")#矩阵散点图查看大致相关情况

在这里插入图片描述
不难发现:
displacement与horsepower、weight呈线性正相关,与acceleration呈现负相关;
horsepower与weight呈现线性,与acceleration、year呈现负相关正相关;
mpg与horsepower、weight呈负相关,与acceleration呈现正相关。

(f) Suppose that we wish to predict gas mileage (mpg) on the basis of the other variables. Do your plots suggest that any of the other variables might be useful in predicting mpg? Justify your answer.
根据(e)问,可探索mpg与weight和year的线性关系:

fm=lm(mpg~weight+year,Auto)
fm

在这里插入图片描述
这里得到的系数表明mpg与weight负相关,与year正相关,印证了我们的想法。

3.3 第十题

本题是对于Boston数据集进行数据分析:
(a)载入Boston数据集,Boston数据集有多少行/列,行/列代表什么?

library(MASS)
Boston
dim(Boston)

506行,14列,分别表示Boston郊区的506个居住价值,14种特征。
(b)对预测变量做散点图,描述发现

pairs(Boston)

在这里插入图片描述
不难发现:crim与age ,dis ,rad ,tax ,ptratio 相关,
zn:与indus, nox, age, lstat相关,
indus与age, dis相关,
nox:与age, dis相关,
dis:与lstat相关,
lstat:与medv相关;

(c ) 解释预测变量与人均犯罪比例的关系
crim与age ,dis ,rad ,tax ,ptratio 相关,

fm=lm(crim~age+dis+rad+tax+ptratio,Boston)
fm

在这里插入图片描述
做线性拟合,也可发现,tax,age,rad与犯罪率正相关,dis,ptratio与犯罪率负相关。

(d)Boston郊外的犯罪率,税率,师生比

par(mfrow=c(1,3))
hist(Boston$crim[Boston$crim>1], breaks=25)
hist(Boston$tax, breaks=25)
hist(Boston$ptratio, breaks=25)

在这里插入图片描述

做频数直方图发现,大部分地方犯罪率较低,但也有超过80的;
大部分地区税率较低,但也有相当一部分郊区税率高(> 600);
师生比偏向于高比例,但没有特别高的;

(e)郊区有多少在查尔斯河附近

dim(subset(Boston, chas == 1))

35个郊区在查尔斯河附近。

(f)城镇师生比中位数

median(Boston$ptratio) #城镇师生比中位数为19.05

(g)业主自用房中位数最小的Boston郊区,该郊区其他预测变量的取值,分布水平,对统计结果描述你的发现

 t(subset(Boston, medv == min(Boston$medv))) #t()用来取转置

在这里插入图片描述
各特征与所有地区的总特征相比,该地区算不上最好的,但也不差。

(h)有多少郊区居民平均居住房间数量超过7个/8个,讨论超过8个的特征

dim(subset(Boston, rm > 7)) #64个
dim(subset(Boston, rm > 8)) #13个
summary(subset(Boston, rm > 8))
summary(Boston)

在这里插入图片描述
在这里插入图片描述

对两种结果进行比较,不难发现超过8个房间的地区具有更低的犯罪率和总体状态。

4. 实验总结

4.1 实验结论

经三道题的练习,对数据进行了各种分析。

4.2 实验收获

学会了R语言统计分析的技能,对R语言基本语法和基本绘图函数能够熟悉应用。掌握了描述性统计方法以理解数据基本特征,运用推断性统计检验总体假设,进行功效分析以确定样本需求的技巧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值