##### 一、实际问题描述 #####
# 制作含有标题和坐标轴名称的折线图
rm(list=ls())
data1=read.table("C:/Users/HP/Desktop/统计案例汇总/房地产行业数据.txt",header=T)
house=data1[,1]
year=data1[,2]
plot(year,house,xlab="年份(年)",type="l",col="black",
main=list("1978至2012年我国房地产行业总产值占GDP比值的折线图",cex=1,font=2),ylab="房地产行业总产值占GDP的比重（%）")

##### 五、统计描述 #####

data2=read.csv("C:/Users/HP/Desktop/统计案例汇总/data1.csv",header=T)
attach(data2)
picture1=hist(price,main=list("广州市二手房房价的分布直方图"),cex.main=1,col="light green",xlab="房价(元/m2)",ylab="频数")
picture2=boxplot(price~region,main=list("广州市各行政区域所对应的房价箱线图"),
         cex.main=1,col="light green",xlab="行政区域",ylab="区域平均价格(元/m2)")                            # 行政区域对房价的影响
hist(years,col="light green",main=list("广州市二手房楼龄的分布直方图"),cex.main=1,xlab="楼龄（年）",ylab="频数")

mp=mean(price)
plot(years,mp,xlab="楼龄(年)",type="l",col="black",main=list("广州市二手房不同楼龄间平均房价的折线图"),cex.main=1,font.main=2,ylab="不同楼龄所对应的的平均房价（元/m2）")
boxplot(price~style,main=list("各类户型所对应的房价的箱线图"),cex.main=1,col="light green",xlab="户型",ylab="房价（元/m2）")
boxplot(price~direction,main=list("各类朝向所对应的房价的箱线图"),cex.main=1,col="light green",xlab="朝向",ylab="房价（元/m2）")
par(mfcol=c(1,2))
hist(floor,main=list("楼层数的分布直方图"),cex.main=1,col="light green",xlab="楼层数",ylab="频数")
hist(stairs,main=list("所在楼层的分布直方图"),cex.main=1,col="light green",xlab="所在楼层",ylab="频数")

par(mfrow=c(3,3))
boxplot(price~elevator,main=list("是否有电梯相对应的房价"),cex.main=1,col="light green",xlab="电梯",ylab="房价（元/m2）")
boxplot(price~fitment,main=list("是否精装修相对应的房价"),cex.main=1,col="light green",xlab="精装修",ylab="房价（元/m2）")
boxplot(price~subway,main=list("是否临近地铁相对应的房价"),cex.main=1,col="light green",xlab="地铁",ylab="房价（元/m2）")
boxplot(price~degree,main=list("是否学位房相对应的房价"),cex.main=1,col="light green",xlab="学位房",ylab="房价（元/m2）")
boxplot(price~sunshade,main=list("是否遮阳相对应的房价"),cex.main=1,col="light green",xlab="遮阳",ylab="房价（元/m2）")
boxplot(price~certificate,main=list("房产证是否过5年相对应的房价"),cex.main=1,col="light green",xlab="业主唯一房产",ylab="房价（元/m2）")
boxplot(price~only,main=list("是否唯一住房相对应的房价"),cex.main=1,col="light green",xlab="产证过5年",ylab="房价（元/m2）")
boxplot(price~parking,main=list("是否有车位相对应的房价"),cex.main=1,col="light green",xlab="车位",ylab="房价（元/m2）")
boxplot(price~river,main=list("是否临江相对应的房价"),cex.main=1,col="light green",xlab="临江",ylab="房价（元/m2）")

par(mfrow=c(1,1))
hist(green,main=list("广州市二手房所在小区的绿化率分布直方图"),cex.main=1,col="light green",xlab="绿化率（%）",ylab="频数")
hist(fee,main=list("广州市二手房所在小区的物业费的分布直方图"),cex.main=1,col="light green",xlab="每月物业费（元/m2）",ylab="频数")

plot(direction)
boxplot(price~direction,main="朝向对房价的影响",col="light green")
summary(plot(direction))
summary(direction)
lines(area,price)
summary(area)
hist(area)

##### 六、统计建模#####

#### （一）协方差分析 ####

### 1.不考虑交互作用协方差分析 ###

rm(list=ls())  
library(car)
data3=read.csv("C:/Users/HP/Desktop/统计案例汇总/data2.csv",header=T)
attach(data3)
lm1=lm(price~as.factor(road)+years+as.factor(style)+as.factor(direction)+area+floor+
stairs+as.factor(elevator)+as.factor(fitment)+as.factor(subway)+as.factor(degree)+as.factor(sunshade)+
as.factor(certificate)+as.factor(only)+as.factor(parking)+as.factor(river)+green+fee,data=data3)
anova(lm1)

lm2=lm(price~as.factor(road)+years+as.factor(direction)+area+floor
+as.factor(elevator)+as.factor(fitment)+as.factor(subway)+as.factor(sunshade)+as.factor(parking)
+as.factor(river)+green+fee,data=data3)
anova(lm2)
summary(lm2)

### 2.考虑交互作用协方差分析 ###

lm3=lm(price~as.factor(road)+years+as.factor(style)+as.factor(direction)+
area+floor+stairs+as.factor(elevator)+as.factor(fitment)+as.factor(subway)
+as.factor(degree)+as.factor(sunshade)+as.factor(certificate)+as.factor(only)+as.factor(parking)
+as.factor(river)+green+fee+as.factor(only)*years+as.factor(certificate)*years
+as.factor(elevator)*stairs+as.factor(degree)*years,data=data3)
anova(lm3)

lm4=lm(price~as.factor(road)+years+as.factor(direction)+
area+floor+stairs+as.factor(elevator)+as.factor(fitment)+as.factor(subway)
+as.factor(sunshade)+as.factor(parking)+as.factor(river)+green+fee+as.factor(elevator)*stairs,data=data3)
anova(lm4)
summary(lm4)

#### 七、Lasso程序 ####

data4=read.csv("C:/Users/HP/Desktop/统计案例汇总/data3.csv",header=T)
attach(data4)
library(grpreg)
library(ncvreg)
X=as.matrix(data4[,c(2:15)])
y=data4$price
group <- c(1:14)
lcol=c('yellow1','tomato3','red',"violetred",
"tan3","steelblue2","springgreen","slategray3" ,"seagreen4", 
"pink2","blue","darkviolet",'black',"cyan1"  )
fit <- grpreg(X, y, group, penalty="grLasso")
labs=c("1-years","2-direction","3-area","4-floor","5-stairs",
"6-elevator","7-fitment","8-subway","9-sunshade",
"10-parking","11-river","12-green","13-fee","14-region")
plot(fit, legend.loc="topleft", lwd=3, alpha=0.0002, legend=labs,col=lcol,xlim=c(6000,-1000))
cvfit <- cv.ncvreg(X,y,penalty="lasso")
fit <- cvfit$fit
beta <- fit$beta[,cvfit$min]
beta
text(-100,-66.78885,"1")
text(-300,36.81246,"2")
text(-450,19.72479,"3")
text(-100,38.62028,"4")
text(-1000,-15.04620,"5")
text(-300,1025.31955,"6")
text(-300,810.81458,"7")
text(-300,601.57785,"8")
text(-300,-736.41805,"9")
text(-300,-849.57336,"10")
text(-300,1456.66018,"11")
text(-700,11.29531,"12")
text(-300,1841.43529,"13")
text(-300,2382.01185,"14")


 
