《统计学习导论-基于R应用》第二章:统计学习(代码)

A = matrix(seq(1,16),4,4)
A
1 5 913
2 6 1014
3 7 1115
4 8 1216
A[1,]
  1. 1
  2. 5
  3. 9
  4. 13
A[-c(1,3),]
2 6 1014
4 8 1216
dim(A)
  1. 4
  2. 4

1.数据读取、查看

getwd()

‘E:/机器学习与量化交易研究课题/统计学习导论-基于R应用/ISLR/data’

Auto = read.table("Auto.data")
fix(Auto)
Auto = read.table("Auto.data",header= T,na.strings="?")
fix(Auto)
dim(Auto)
  1. 397
  2. 9
Auto = read.csv("Auto.csv",header= T, na.strings="?")
fix(Auto)
dim(Auto)
  1. 397
  2. 9
Auto[1:5,]
mpgcylindersdisplacementhorsepowerweightaccelerationyearoriginname
18 8 307 130 3504 12.0 70 1 chevrolet chevelle malibu
15 8 350 165 3693 11.5 70 1 buick skylark 320
18 8 318 150 3436 11.0 70 1 plymouth satellite
16 8 304 150 3433 12.0 70 1 amc rebel sst
17 8 302 140 3449 10.5 70 1 ford torino
Auto = na.omit(Auto) # 剔除空行
dim(Auto)
  1. 392
  2. 9
names(Auto) # 查看变量名(列名/特征)
  1. 'mpg'
  2. 'cylinders'
  3. 'displacement'
  4. 'horsepower'
  5. 'weight'
  6. 'acceleration'
  7. 'year'
  8. 'origin'
  9. 'name'
plot(mpg,cylinders) # R 不会默认数据集,需要指定
Error in plot(mpg, cylinders): 找不到对象'mpg'
Traceback:
1. plot(mpg, cylinders)
# 指定方式一
plot(Auto$mpg,Auto$cylinders)

在这里插入图片描述

# 指定方式二
attach(Auto)
plot(mpg, cylinders)
The following objects are masked from Auto (pos = 3):

    acceleration, cylinders, displacement, horsepower, mpg, name,
    origin, weight, year

The following objects are masked from Auto (pos = 4):

    acceleration, cylinders, displacement, horsepower, mpg, name,
    origin, weight, year

The following objects are masked from Auto (pos = 5):

    acceleration, cylinders, displacement, horsepower, mpg, name,
    origin, weight, year

The following objects are masked from Auto (pos = 6):

    acceleration, cylinders, displacement, horsepower, mpg, name,
    origin, weight, year

在这里插入图片描述

箱线图

# 将定量变量转变为定性变量(虽然是数值,但是种类比较少)
cylinders = as.factor(cylinders)
# plot()绘制的是散点图,如果X轴输入是定性变量,自动转为箱线图
plot(cylinders,mpg)
plot(cylinders,mpg,col="red")

在这里插入图片描述

在这里插入图片描述

plot(cylinders,mpg,col="red", varwidth= T) # varwidth是一个逻辑值。 设置为true以绘制与样本大小成比例的框的宽度。

在这里插入图片描述

plot(cylinders,mpg,col="red", varwidth= T, horizontal=T)
plot(cylinders,mpg,col="red", varwidth= T, xlab= "cylinders", ylab= "MPG")

在这里插入图片描述

在这里插入图片描述

直方图

hist(mpg)
hist(mpg, col = 2)

在这里插入图片描述

在这里插入图片描述

hist(mpg, col= 2, breaks = 15) # 建议的分组组数,标量R不一定会用,如需精确breaks= seq(1,6,0.5)

在这里插入图片描述

两两绘图

pairs(Auto) # 数据集中两两变量绘制散点图

在这里插入图片描述

pairs(~mpg+displacement+horsepower,Auto) # 指定子集

在这里插入图片描述

显示图中某点的某些信息

plot(horsepower,mpg)
identify(horsepower,mpg,name)

在这里插入图片描述

描述统计量

summary(Auto)
      mpg          cylinders      displacement     horsepower        weight    
 Min.   : 9.00   Min.   :3.000   Min.   : 68.0   Min.   : 46.0   Min.   :1613  
 1st Qu.:17.00   1st Qu.:4.000   1st Qu.:105.0   1st Qu.: 75.0   1st Qu.:2225  
 Median :22.75   Median :4.000   Median :151.0   Median : 93.5   Median :2804  
 Mean   :23.45   Mean   :5.472   Mean   :194.4   Mean   :104.5   Mean   :2978  
 3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:275.8   3rd Qu.:126.0   3rd Qu.:3615  
 Max.   :46.60   Max.   :8.000   Max.   :455.0   Max.   :230.0   Max.   :5140  
                                                                               
  acceleration        year           origin                      name    
 Min.   : 8.00   Min.   :70.00   Min.   :1.000   amc matador       :  5  
 1st Qu.:13.78   1st Qu.:73.00   1st Qu.:1.000   ford pinto        :  5  
 Median :15.50   Median :76.00   Median :1.000   toyota corolla    :  5  
 Mean   :15.54   Mean   :75.98   Mean   :1.577   amc gremlin       :  4  
 3rd Qu.:17.02   3rd Qu.:79.00   3rd Qu.:2.000   amc hornet        :  4  
 Max.   :24.80   Max.   :82.00   Max.   :3.000   chevrolet chevette:  4  
                                                 (Other)           :365  
summary(mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   9.00   17.00   22.75   23.45   29.00   46.60 
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值