2# 楚韵荆风
> install.packages("np")
--- Please select a CRAN mirror for use in this session ---
警告: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.5
试开URL’http://ftp.ctex.org/mirrors/CRAN ... b/2.5/np_0.13-1.zip
Content type 'application/zip' length 877145 bytes
打开了URL
downloaded 856Kb
package 'np' successfully unpacked and MD5 sums checked
The downloaded packages are in
C:\Documents and Settings\Administrator\Local Settings\Temp\RtmpkkEDAb\downloaded_packages
updating HTML package descriptions
> library(np)
载入需要的程辑包:boot
Nonparametric Kernel Methods for Mixed Datatypes (version 0.13-1)
Warning message:
程辑包'np'是用R版本2.5.1 来建造的
> library(boot)
> install.packages("cubature")
警告: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.5
Warning message:
package 'cubature' is not available
> library(cubature)
错误于library(cubature) : 不存在叫'cubature'这个名字的程辑包
>
> # 把数据文件放在根目录文件的下面,或指定你的根目录到数据文件所在的文件夹
> dat=read.table("income.txt",head=TRUE)
> cuh=dat$income
> icup=dat$payment
> plot(cuh,icup,xlab="城镇居民的可支配收入",ylab="城镇居民的消费支出")
> # 使用非参数h核回归方法,通过最小二乘交叉验证方法(cv.ls)选择最优带宽,核函数为高斯核
> bw1=npregbw(icup~cuh,regtype="lc",bwmethod="cv.ls",bwscaling=FALSE,
+ ckerorder=2,ckertype="gaussian")$bw # 最优带宽 bw=942.80058
> fit=npreg(icup~cuh,bws=bw1, gradients=TRUE,residuals = TRUE,regtype="ll")
错误于toFrame(txdat) : txdat must be a data frame, matrix, vector, or factor
> summary(fit) # 拟合优度 R^2=0.999914
错误于summary(fit) : 找不到这个对象"fit"
> fitted(fit) # 拟合值
错误于fitted(fit) : 找不到这个对象"fit"
> [1] 1284.667 1432.575 1687.863 2124.774 2859.107 3483.401 3910.878
错误: syntax error, unexpected '['在"["里
> [8] 4148.837 4340.685 4647.390 4951.708 5369.195 5977.431 6522.083
错误: syntax error, unexpected '['在"["里
> [15] 7182.792 7907.363 8706.570 9994.640 11267.518 12264.079
错误: syntax error, unexpected '['在"["里
> resid(fit) # 残差
错误于resid(fit) : 找不到这个对象"fit"
> mse=sum(resid(fit)^2);mse # 残差平方和 17055.17
错误于resid(fit) : 找不到这个对象"fit"
>
> # 用最小二乘线性回归
> fit2=lm(icup~cuh)
> summary(fit2) # 拟合优度 R^2= 0.9978
Call:
lm(formula = icup ~ cuh)
Residuals:
Min 1Q Median 3Q Max
-234.11 -156.76 65.64 127.45 193.31
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.586e+02 6.518e+01 7.036 1.45e-06 ***
cuh 6.982e-01 7.657e-03 91.181 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 153.8 on 18 degrees of freedom
Multiple R-Squared: 0.9978, Adjusted R-squared: 0.9977
F-statistic: 8314 on 1 and 18 DF, p-value: < 2.2e-16
> mse2=sum(resid(fit2)^2);mse2 # 残差平方和 425898.2
[1] 425898.2
>
> # 从残差平方和可以说明非参数回归方法比最小二乘回归方法你和结果要好
>
这个 怎么解决啊 谢谢啦