全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
6246 1
2014-05-16


1.研究问题:

汽车已经成为了人们出行必不可少的工具之一。现在各个汽车销售商也是想方设法提高汽车的销售量。但汽车的销售量是由什么决定的呢?说道底,汽车的销售量是由客户的满意度来决定的。我们又想问,汽车的满意度是主要由什么决定的呢?汽车的价格,质量,舒服度哪个因素起的作用更大呢?因此,本作业将侧重探讨影响汽车满意度的因素

2.数据介绍:

buy: buying price(购买价格,分为low, med, high, vhigh

main: price of the maintenance(保养价格,分为low, med, high, vhigh

doors: number of doors(门的个数,分为2, 3, 4, 5morecapacity: capacity in terms of persons to carry(载人个数

lug_boot: the size of luggage boot(车身的大小,分为small, med, big

safety: estimated safety of the car(安全程度,分为low, med, high

accept: car acceptability(被接受程度,四个等级:unacc, acc, good, vgood

我们根据buy, main,doors, capacity, lug_boot, safety来预测accept的值,其中自变量和因变量为均为有序变量,因此,我们需要使用广义线性模型中的次序logti回归模型(简称“oligit回归”)来进行拟合。因此,建立模型如下:


其中i表示第i个样本。

3.数据处理:

1)数据输入及整理

#设定工作路径

setwd("E:/R&计量经济学/R/大作业/汽车满意度/train")

#读入数据

data=read.table('train.txt',F,sep=',')

#设定变量名

#ologit回归的因变量需要设置因子

data$accept1=factor(data$accept1)

#自变量buy编码形成新变量buy1,由低到高1-4编码

data$buy

for(i in 1:1042){

if(data$buy=='low')data$buy1=1

else if(data$buy=='med')data$buy1=2

else if(data$buy=='high')data$buy1=3

else if(data$buy=='vhigh')data$buy1=4

}

#自变量main编码形成新变量main1,由低到高1-4编码

data$main

for(i in 1:1042){

if(data$main=='low')data$main1=1

else if(data$main=='med')data$main1=2

else if(data$main=='high')data$main1=3

else if(data$main=='vhigh')data$main1=4

#自变量lug_boot编码形成新变量lug_boot1,由低到高1-3编码

data$lug_boot

for(i in 1:1042){

if(data$lug_boot=='small')data$lug_boot1=1

else if(data$lug_boot=='med')data$lug_boot1=2

else if(data$lug_boot=='big')data$lug_boot1=3


2)训练集的oligit回归

#ologit回归需要使用packages ‘ordinal’‘rms’,前者用来回归,后者可以显示一些拟合指标

library(ordinal)

library(rms)

#clm用来作ologit回归


3)用测试集进行测试

#剔除test中无关的变量,仅保留6个因变量

new=test[,c(9:14)]

#predict进行预测

pre=predict(clm_all,new)

pre

$fit

              1            2            3            4

1  0.999951050 4.873134e-05 1.011350e-07 1.177863e-07

2  0.991292961 8.667759e-03 1.814545e-05 2.113381e-05

3  0.998543071 1.450404e-03 3.014343e-06 3.510657e-06

4  0.999992597 7.370344e-06 1.529548e-08 1.781378e-08

5  0.995939524 4.042243e-03 8.422815e-06 9.809755e-06

#仅保留5行,其余省略

#pre$fit会生成每一个样本对应一个accept1值的概率

#因此,我们需要选择每一行最大的值所对应的序号来作为预测值

#找出最大值的序号

max_fit=apply(as.matrix(pre$fit),1,which.max)

max_fit

max_fit

1   2   3  4   5   6  7   8   9 10  11  12 13  14  15  16

1   1   1  1   1   1  1   1   1  2   1   1  2   1   1   1

17 18  19  20 21  22  23 24  25  26 27  28  29 30  31  32

1   1   1  1   1   1  1   1   1  1   1   2  1   1   2   1

33 34  35  36 37  38  39 40  41  42 43  44  45 46  47  48

1   1   1  1   1   1  1   1   2  1   1   2  1   1   1   1

49 50  51  52 53  54  55 56  57  58 59  60  61 62  63  64

1   1   1  1   1   1  1   2   1  2   2   1  1   1   1   1

65 66  67  68 69  70  71 72  73  74 75  76  77 78  79  80

1   2   1  2   2   1  2   1   1  1   2   2  1   1   1   2

81 82  83  84 85  86  87 88  89  90 91  92  93  94  95  96

1   1   1  2   1   1  1   2   2  4   2   2  4   1   1   2

97 98  99 100 101 102 103 104

1   2   2  1   2   2  1   2

#将其同原有的accept1值进行比对,计算正确预测的概率

sum=0;

for(i in 1:104){

if(max_fit==test$accept1)sum=sum+1}



sum/104

0.8076923

#预测的正确率为80.7%,效果不错。

总结:ologit回归模型是在问卷调查中非常广泛使用的研究工具。事实上,上述数据的质量非常之高,预测效果也不错。在很多研究中,广义线性模型的R2都很难超过0.1



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2014-5-18 23:28:06
请楼主附上数据集。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群