#P120习题4.3 rm(list = ls()) A = read.xlsx("xiti_4.xlsx",sheet = 3) names(A) = c("ord","Y","K","L") attach(A) fm = lm(Y~log(K)+log(L))#线性回归模型 ei = resid(fm) X = cbind(1,as.matrix(A[,3:4])) t = ti(ei,X) #外部学生…
rm(list = ls()) #数据处理 library(openxlsx) library(car) library(lmtest) data = read.xlsx("xiti4.xlsx",sheet = 1) data attach(data) fm1 = lm(y~x1+x2+x3+x4+x5+x6+x7) #多元回归模型 coef(fm1) #残差图:残差分析 ei = resid(fm1) X = cbind(1,as.matrix(data[,2:8])) t = t…
--多项式回归模型 --单变量多项式模型 --多变量多项式模型 rm(list = ls()) library(openxlsx) library(leaps) #单变量多项式模型# data = read.xlsx("table7-1.xlsx") head(data) plot(data$f,data$mw) attach(data) new_data = as.data.frame(cbind(mw,f,f**2,f**3)) names(new_data) = c("…
rm(list = ls()) library(car) library(MASS) library(openxlsx) A = read.xlsx("data141.xlsx") head(A) fm = lm(y~x1+x2+x3+x4 , data=A ) #判断多重共线性 vif(fm) > vif(fm) x1 x2 x3 x4 38.49621 254.42317 46.86839 282.51286 #具有多重共线性 #进行主成分回归 A.pr = princomp…
rm(list = ls()) library(car) library(MASS) library(openxlsx) A = read.xlsx("data140.xlsx") head(A) attach(A) fm = lm(y~x1+x2+x3 , data=A) #建立模型 vif(fm) #查看模型是否存在共线性 > vif(fm) #查看模型是否存在共线性 x1 x2 x3 21.631451 21.894402 1.334751 结果显示存在共线性 summar…
rm(list = ls()) A = read.xlsx("xiti_4.xlsx",sheet = 4) names(A) = c("ord","x","y") #进行回归 attach(A) fm = lm(y~x) summary(fm) coef(fm) #回归残差关于x的散点图 plot(x,resid(fm)) 成发射状,意味着方差随着x的增加而变大 #尝试用加权最小二乘 #先分类(可以聚类.手动分类) plot…
rm(list = ls()) library(openxlsx) library(MASS) data = read.xlsx("xiti_4.xlsx",sheet = 2) data fm = lm(y~x1+x2+x3+x4+x5+x6+x7,data) par(mfrow = c(2,2),mar = 0.4+c(4,4,1,1),oma = c(0,0,2,0)) a1 = boxcox(fm,lambda = seq(0,1,by = 0.1)) #λ=0.76 l =…
BG:在box-cox变换中,当λ = 0时即为对数变换. 当所分析变量的标准差相对于均值而言比较大时,这种变换特别有用.对数据作对数变换常常起到降低数据波动性和减少不对称性的作用..这一变换也能有效消除异方差性 library(MASS) library(openxlsx) data= read.xlsx("data104.xlsx",sheet = 1) #导入数据 attach(data) op<-par(mfrow=c(2,2),mar=0.4+c(4,4,1,1),om…
y,X1,X2,X3 分别表示第 t 年各项税收收入(亿元),某国生产总值GDP(亿元),财政支出(亿元)和商品零售价格指数(%). (1) 建立线性模型: ① 自己编写函数: > library(openxlsx) > data = read.xlsx("22_data.xlsx",sheet = 1) > x = data[,-c(1,2)] > x = cbind(rep(1,17),x) > x_mat = as.matrix(x) > y…
rm(list = ls()) A = read.csv("data115.csv") fm = lm(y~x1+x2,data = A) coef(fm) A.cooks = cooks.distance(fm) #计算cook距离 new_A = cbind(A,A.cooks) #把原始数据与cook距离放在一个数据框中查看 new_A[order(A.cooks,decreasing = T),]#按cook距离降序排列 显示西藏地区数据对应的cook统计量明显过大,不能放入建…