ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 第五课实验

第五课实验

原创 Linux操作系统 作者:DataKW 时间:2012-10-27 14:58:19 0 删除 编辑
> 回归诊断
错误: 找不到对象'回归诊断'
> 回归诊断条件
错误: 找不到对象'回归诊断条件'
> 正态分布检验
错误: 找不到对象'正态分布检验'
> shapiro.test(x1)
        Shapiro-Wilk normality test
data:  x1
W = 0.9488, p-value = 0.3489
> x
[1] 1 2 6
> x1
 [1] 50 11 76 94 31 88 48 65 60 97 81 81 52 15 49 73 73 18 59 54
> x2
[1] 4 5 6
>
>
>
>
> 样本是正态分布,残差也会是正态分布
> x1=rnorm(20,mean=60,sd=1)
> x2=rnorm(20,mean=60,sd=10)
> x1
 [1] 60.48074 59.96494 59.86667 59.76054 59.75673 60.74676 57.93320 58.18842 59.76445 60.06335 59.33858 58.38529 58.82738 58.99790 60.69223
[16] 60.27047 61.43682 59.33643 59.24530 60.49586
> x2
 [1] 83.03489 53.61508 70.05985 61.39542 62.77583 48.62286 72.09769 69.77086 43.08044 78.20685 76.36073 68.96168 74.68398 58.57418 58.17206
[16] 70.13679 69.75078 54.62112 62.87347 64.12008
> x3=runif(20,min=60,max=100)
> x3
 [1] 93.59851 70.59561 92.51652 73.88949 80.40056 72.67123 72.11999 84.86872 88.14778 93.52501 83.95491 90.21105 74.44592 79.61049 62.29222
[16] 98.40356 85.83167 98.99127 60.19403 80.76415
>
> lm.sol=lm(x1,x2)
错误于formula.default(object, env = baseenv()) : 公式不对
> lm.sol=lm(x1~x2)
>
>
>
> lm.sol
Call:
lm(formula = x1 ~ x2)
Coefficients:
(Intercept)           x2 
   60.55443     -0.01348 
>
> y.res=residuals(lm.sol)
> y.res
          1           2           3           4           5           6           7           8           9          10          11          12
 1.04563263  0.13324839  0.25666197  0.03372858  0.04852417  0.84777594 -1.64934464 -1.42548820 -0.20924418  0.56315666 -0.18649457 -1.23952100
         13          14          15          16          17          18          19          20
-0.72029299 -0.76694236  0.92196850  0.66149231  1.82264007 -0.48169657 -0.46158317  0.80577846
> shaprio.test(y.res)
错误: 没有"shaprio.test"这个函数
> shapiro.test(y.res)
        Shapiro-Wilk normality test
data:  y.res
W = 0.9822, p-value = 0.9594
> 回归值和残差关系:正常情况,不随回归值增大而增大;异方差情况;非线形情况
> 非线形情况的残差,模型需要引入非线形情况
> 样本是正态分布,残差也会是正态分布
> 样本是正态分布,模型的残差也会是正态分布
> y=scan() 1,2,3,4
错误: 意外的数值量在"y=scan() 1"里
> y=scan(1,2,3,4)
错误于scan(1, 2, 3, 4) : 'nmax'和'n'两者只能有一个设定
>
>
>
>
> 画出标准化残差散点图
错误: 找不到对象'画出标准化残差散点图'
> y.rst=rstandard(lm.sol)
> y.rst
          1           2           3           4           5           6           7           8           9          10          11          12
 1.27466143  0.15323553  0.28666069  0.03755161  0.05390660  1.01606675 -1.85442710 -1.59089559 -0.26914540  0.65563549 -0.21430787 -1.38075889
         13          14          15          16          17          18          19          20
-0.81955681 -0.86046616  1.03590682  0.73896227  2.03402778 -0.55050941 -0.51272408  0.89413605
> y.fit=predict(lm.sol)
> y.fit
       1        2        3        4        5        6        7        8        9       10       11       12       13       14       15       16
59.43511 59.83169 59.61001 59.72681 59.70820 59.89899 59.58254 59.61391 59.97370 59.50019 59.52507 59.62482 59.54768 59.76484 59.77026 59.60897
      17       18       19       20
59.61418 59.81813 59.70689 59.69008
> plot(r.rst~y.fit)
错误于eval(expr, envir, enclos) : 找不到对象'r.rst'
> plot(y.rst~y.fit)
> lm.sol
Call:
lm(formula = x1 ~ x2)
Coefficients:
(Intercept)           x2 
   60.55443     -0.01348 
> 回归诊断两种分析:正态分布检验;残差分析;
> plot(y.rst~y.fit)
> abline(0.1,0.5)
> abline(-0.1,-0.5)
> plot(y.rst~y.fit)
>
>
>
> lm.new=update(lm.sol,sql(.)~.)
错误于eval(expr, envir, enclos) : 没有"sql"这个函数
> lm.new=update(lm.sol,sqr(.)~.)
错误于eval(expr, envir, enclos) : 没有"sqr"这个函数
> lm.new=update(lm.sol,sqrt(.)~.)
> lm.nes
错误: 找不到对象'lm.nes'
> lm.new
Call:
lm(formula = sqrt(x1) ~ x2)
Coefficients:
(Intercept)           x2 
  7.7820668   -0.0008787 
> summary(lm.new)
Call:
lm(formula = sqrt(x1) ~ x2)
Residuals:
      Min        1Q    Median        3Q       Max
-0.107332 -0.034930  0.002856  0.045269  0.117384
Coefficients:
              Estimate Std. Error t value Pr(>|t|)   
(Intercept)  7.7820668  0.0885092  87.924   <2e-16 ***
x2          -0.0008787  0.0013451  -0.653    0.522   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.0599 on 18 degrees of freedom
Multiple R-squared: 0.02316,    Adjusted R-squared: -0.03111
F-statistic: 0.4267 on 1 and 18 DF,  p-value: 0.5219
> coef(lm.new)
  (Intercept)            x2
 7.7820667716 -0.0008786541
> lm.nes
错误: 找不到对象'lm.nes'
> lm.new
Call:
lm(formula = sqrt(x1) ~ x2)
Coefficients:
(Intercept)           x2 
  7.7820668   -0.0008787 
> z=lm(iris$Sepal.Length~iris$Sepal.Width)
> plot(z)
等待页面改变的确认...
等待页面改变的确认...
等待页面改变的确认...
等待页面改变的确认...
> plot画函数能画出很多用用的关系图出来
错误: 找不到对象'plot画函数能画出很多用用的关系图出来'
> 多重共线形
错误: 找不到对象'多重共线形'
>
>
>
> x1
 [1] 60.48074 59.96494 59.86667 59.76054 59.75673 60.74676 57.93320 58.18842
 [9] 59.76445 60.06335 59.33858 58.38529 58.82738 58.99790 60.69223 60.27047
[17] 61.43682 59.33643 59.24530 60.49586
> x2
 [1] 83.03489 53.61508 70.05985 61.39542 62.77583 48.62286 72.09769 69.77086
 [9] 43.08044 78.20685 76.36073 68.96168 74.68398 58.57418 58.17206 70.13679
[17] 69.75078 54.62112 62.87347 64.12008
> x3
 [1] 93.59851 70.59561 92.51652 73.88949 80.40056 72.67123 72.11999 84.86872
 [9] 88.14778 93.52501 83.95491 90.21105 74.44592 79.61049 62.29222 98.40356
[17] 85.83167 98.99127 60.19403 80.76415
> x4=rnorm(20,mean=50,sd=4)
> x5=rnorm(20,mean=50,sd=40)
> x6=runif(20,min=10,max=100)
> x7=runif(20,min=10,max=100)
> xx=cor(collinear[2:7])
错误于is.data.frame(x) : 找不到对象'collinear'
> collinear=data.frame(x1,x2,x3,x4,x5,x6,x7)
> xx=cor(collinear[2:7])
> xx
            x2         x3          x4          x5          x6          x7
x2  1.00000000  0.2965428 -0.29446080 -0.01012002  0.29360875  0.27657302
x3  0.29654280  1.0000000 -0.24468271  0.18034901  0.15907494  0.33305637
x4 -0.29446080 -0.2446827  1.00000000  0.05542283 -0.26835449 -0.22279817
x5 -0.01012002  0.1803490  0.05542283  1.00000000  0.08022103 -0.09827330
x6  0.29360875  0.1590749 -0.26835449  0.08022103  1.00000000  0.01059932
x7  0.27657302  0.3330564 -0.22279817 -0.09827330  0.01059932  1.00000000
> k=kappa(xx,exact=TRUE)
> k
[1] 3.51747
> k大于1000有严重共线性,100和1000直接有较强共线性,小于100共线性程度弱
> eign(xx)
错误: 没有"eign"这个函数
> eigen(xx)
$values
[1] 1.9792595 1.1315242 1.0201693 0.7058156 0.6005374 0.5626941
$vectors
            [,1]        [,2]       [,3]        [,4]       [,5]        [,6]
[1,] -0.50748501  0.03882625 -0.1349678  0.58692133  0.6079967 -0.09272828
[2,] -0.47590969 -0.17019058  0.4568564 -0.07685827 -0.3111278 -0.65811827
[3,]  0.46198949 -0.11337518  0.2909249  0.77332207 -0.2971102 -0.05265915
[4,] -0.04497205 -0.81120015  0.3507334 -0.10246577  0.2489970  0.38002518
[5,] -0.36418831 -0.34871372 -0.5972155  0.18627041 -0.5659250  0.18474703
[6,] -0.40973043  0.42074556  0.4568899  0.07995763 -0.2509868  0.61396924
> 特征值小的,特征向量,除去系数小的,剩下的就是有共线性的关系
>
>
> 广义线性模型
错误: 找不到对象'广义线性模型'
> glm.sol=glm(x1~x2,family=binomial,data=collinear)
错误于eval(expr, envir, enclos) : y值必需满足0 <= y <= 1
> x1=round(x1)
> glm.sol=glm(x1~x2,family=binomial,data=collinear)
错误于eval(expr, envir, enclos) : y值必需满足0 <= y <= 1
> glm.sol=glm(x1~x2,family=binomial)
错误于eval(expr, envir, enclos) : y值必需满足0 <= y <= 1
> x2=round(x2)
> glm.sol=glm(x1~x2,family=binomial,data=collinear)
错误于eval(expr, envir, enclos) : y值必需满足0 <= y <= 1
> x1
 [1] 60 60 60 60 60 61 58 58 60 60 59 58 59 59 61 60 61 59 59 60
> x1[x1>60]=1
> x1
 [1] 60 60 60 60 60  1 58 58 60 60 59 58 59 59  1 60  1 59 59 60
> x1[x1<50]=0
> x1
 [1] 60 60 60 60 60  0 58 58 60 60 59 58 59 59  0 60  0 59 59 60
> x1[x1>50]=1
> x1
 [1] 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 0 1 1 1
>
>
> glm.sol=glm(x1~x2,family=binomial,data=collinear)
错误于eval(expr, envir, enclos) : y值必需满足0 <= y <= 1
> glm.sol=glm(x1~x2,family=binomial)
> glm.sol
Call:  glm(formula = x1 ~ x2, family = binomial)
Coefficients:
(Intercept)           x2 
   -2.85114      0.07317 
Degrees of Freedom: 19 Total (i.e. Null);  18 Residual
Null Deviance:      16.91
Residual Deviance: 15.59        AIC: 19.59
> summary(glm.sol)
Call:
glm(formula = x1 ~ x2, family = binomial)
Deviance Residuals:
    Min       1Q   Median       3Q      Max 
-2.1767   0.3528   0.4433   0.5737   1.0549 
Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.85114    4.03559  -0.706    0.480
x2           0.07317    0.06589   1.110    0.267
(Dispersion parameter for binomial family taken to be 1)
    Null deviance: 16.908  on 19  degrees of freedom
Residual deviance: 15.586  on 18  degrees of freedom
AIC: 19.586
Number of Fisher Scoring iterations: 5
> glm.sol=glm(x3~x2,family=binomial)
错误于eval(expr, envir, enclos) : y值必需满足0 <= y <= 1
> glm.sol=glm(x1~x2+x3,family=binomial)
> summary(glm.sol)
Call:
glm(formula = x1 ~ x2 + x3, family = binomial)
Deviance Residuals:
    Min       1Q   Median       3Q      Max 
-2.4005   0.2084   0.3450   0.5438   1.0092 
Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept) -8.33615    6.66186  -1.251    0.211
x2           0.06609    0.07580   0.872    0.383
x3           0.07611    0.06636   1.147    0.251
(Dispersion parameter for binomial family taken to be 1)
    Null deviance: 16.908  on 19  degrees of freedom
Residual deviance: 14.089  on 17  degrees of freedom
AIC: 20.089
Number of Fisher Scoring iterations: 5
> glm.sol=glm(x3~x2,family=binomial)
错误于eval(expr, envir, enclos) : y值必需满足0 <= y <= 1
> glm.sol=glm(x1~x2,family=binomial)
> summary(glm.sol)
Call:
glm(formula = x1 ~ x2, family = binomial)
Deviance Residuals:
    Min       1Q   Median       3Q      Max 
-2.1767   0.3528   0.4433   0.5737   1.0549 
Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.85114    4.03559  -0.706    0.480
x2           0.07317    0.06589   1.110    0.267
(Dispersion parameter for binomial family taken to be 1)
    Null deviance: 16.908  on 19  degrees of freedom
Residual deviance: 15.586  on 18  degrees of freedom
AIC: 19.586
Number of Fisher Scoring iterations: 5
> plot(glm.sol)
等待页面改变的确认...
等待页面改变的确认...
等待页面改变的确认...
等待页面改变的确认...
>
>
>
> norell=data.frame(x=0:5,n=rep(70,6,success=c(0,9,21,47,60,63)))
> norell$Ymat=cbind(norell)
>
>
> norell$Ymat=cbind(norell$success,norell$n-norell$success)
错误于`$<-.data.frame`(`*tmp*`, "Ymat", value = numeric(0)) :
  replacement has 0 rows, data has 6
> norell$Ymat<-cbind(norell$success,norell$n-norell$success)
错误于`$<-.data.frame`(`*tmp*`, "Ymat", value = numeric(0)) :
  replacement has 0 rows, data has 6
> 怎么画拟合曲线和观测值在一个图上?????????????
>
> 非线性模型
错误: 找不到对象'非线性模型'
> x=c(1.5,2.8,4.5,7.5,10.5,13.5,15.1,16.5,19.5,22.5,24.5,26.5)
> y=c(7.0,5.5,4.6,3.6,2.9,2.7,2.5,2.4,2.2,2.1,1.9,1.8)
> plot(x,y)
> lm.l=lm(y-x)
错误于formula.default(object, env = baseenv()) : 公式不对
> lm.l=lm(y~x)
> summary(lm.l)
Call:
lm(formula = y ~ x)
Residuals:
    Min      1Q  Median      3Q     Max
-0.9179 -0.5537 -0.1628  0.3953  1.6519
Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept)  5.60316    0.43474  12.889 1.49e-07 ***
x           -0.17003    0.02719  -6.254 9.46e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.7701 on 10 degrees of freedom
Multiple R-squared: 0.7964,     Adjusted R-squared: 0.776
F-statistic: 39.11 on 1 and 10 DF,  p-value: 9.456e-05
> 对数法
错误: 找不到对象'对数法'
> lm.log=lm(y~log(x))
> summary(lm.log)
Call:
lm(formula = y ~ log(x))
Residuals:
     Min       1Q   Median       3Q      Max
-0.33291 -0.10133 -0.04693  0.16512  0.34844
Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept)   7.3639     0.1688   43.64 9.60e-13 ***
log(x)       -1.7568     0.0677  -25.95 1.66e-10 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.2064 on 10 degrees of freedom
Multiple R-squared: 0.9854,     Adjusted R-squared: 0.9839
F-statistic: 673.5 on 1 and 10 DF,  p-value: 1.66e-10
> plot(x,y)
> lines(x,fitted(lm.log))y(lm.log)
错误: 意外的符号在"lines(x,fitted(lm.log))y"里
> lines(x,fitted(lm.log))
> plot(x,y)
> lines(x,fitted(lm.log))
> 画拟合曲线和观测值在一个图上!!!!!!!11
错误: 意外的'!'在"画拟合曲线和观测值在一个图上!"里
>
>
>
> 指数法
错误: 找不到对象'指数法'
> lm.exp=(log(y)~x)
> lm.exp=lm(log(y)~x)
> lines(x,fitted(lm.exp))
> summary(lm.exp)
Call:
lm(formula = log(y) ~ x)
Residuals:
     Min       1Q   Median       3Q      Max
-0.18246 -0.10664 -0.01670  0.08079  0.25946
Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept)  1.759664   0.075101   23.43 4.54e-10 ***
x           -0.048809   0.004697  -10.39 1.12e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.133 on 10 degrees of freedom
Multiple R-squared: 0.9153,     Adjusted R-squared: 0.9068
F-statistic:   108 on 1 and 10 DF,  p-value: 1.116e-06
>
>
>
> 幂函数法
错误: 找不到对象'幂函数法'
>
>
>
> lm.pow=lm(log(y)~log(x))
> summary(lm.pow)
Call:
lm(formula = log(y) ~ log(x))
Residuals:
      Min        1Q    Median        3Q       Max
-0.054727 -0.020805  0.004548  0.024617  0.045896
Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept)  2.19073    0.02951   74.23 4.81e-15 ***
log(x)      -0.47243    0.01184  -39.90 2.34e-12 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.0361 on 10 degrees of freedom
Multiple R-squared: 0.9938,     Adjusted R-squared: 0.9931
F-statistic:  1592 on 1 and 10 DF,  p-value: 2.337e-12
> lines(x,fitted(lm.pow))
> lines(x,exp(fitted(lm.exp)))
> lines(x,exp(fitted(lm.exp)))
> lines(x,exp(fitted(lm.exp)))
错误于plot.xy(xy.coords(x, y), type = type, ...) : 还没有调用plot.new
> plot(x,y)
> lines(x,exp(fitted(lm.exp)))
> plot(x,y)
> lines(x,exp(fitted(lm.pow)))
>
>
>
> 多项式回归
错误: 找不到对象'多项式回归'
> x1=x
> x1
 [1]  1.5  2.8  4.5  7.5 10.5 13.5 15.1 16.5 19.5 22.5 24.5 26.5
> x2=x^2
> x2
 [1]   2.25   7.84  20.25  56.25 110.25 182.25 228.01 272.25 380.25 506.25
[11] 600.25 702.25
> lm.2=lm(y~x1+x2)
> summary(lm.2)
Call:
lm(formula = y ~ x1 + x2)
Residuals:
     Min       1Q   Median       3Q      Max
-0.43718 -0.31604  0.02362  0.22211  0.75956
Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept)  6.914687   0.331987  20.828 6.35e-09 ***
x1          -0.465631   0.056969  -8.173 1.86e-05 ***
x2           0.010757   0.002009   5.353  0.00046 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.3969 on 9 degrees of freedom
Multiple R-squared: 0.9513,     Adjusted R-squared: 0.9405
F-statistic: 87.97 on 2 and 9 DF,  p-value: 1.237e-06
>
>
> plot(x,y)
> lines(x,fitted(lm.2))
> 多项式容易出现过度拟合的问题
错误: 找不到对象'多项式容易出现过度拟合的问题'
> poly(x,degree=2)
                1           2
 [1,] -0.43217421  0.42003237
 [2,] -0.38627960  0.26745002
 [3,] -0.32626358  0.09374311
 [4,] -0.22035295 -0.14140313
 [5,] -0.11444232 -0.28540636
 [6,] -0.00853169 -0.33826656
 [7,]  0.04795398 -0.32919130
 [8,]  0.09737894 -0.29998375
 [9,]  0.20328957 -0.17055792
[10,]  0.30920020  0.05001093
[11,]  0.37980729  0.24769184
[12,]  0.45041437  0.48588076
attr(,"degree")
[1] 1 2
attr(,"coefs")
attr(,"coefs")$alpha
[1] 13.74167 13.73815
attr(,"coefs")$norm2
[1]     1.0000    12.0000   802.3492 39003.0164
attr(,"class")
[1] "poly"   "matrix"
> poly函数理解
错误: 找不到对象'poly函数理解'
> nls函数对非线性参数的估计?????????????
>

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/27573546/viewspace-747385/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 第四课实验
下一篇: 第六课实验2
请登录后发表评论 登录
全部评论

注册时间:2012-08-12

  • 博文量
    132
  • 访问量
    331526