with(stats) 调入统计软件包 统计包中又有7个子包: (1)describe描述性数据分析 (2)fit拟合回归分析 (3)transform数据形式变换 (4) random按分布产生随机数 (5)statevalf分布的数值计算 (6) statplots统计绘图 (7)anova方差分析
第一节 描述性数据分析discribe
格式 describe[函数](数据) 有22个描述性统计量函数: 平均值mean方差variance标准差standarddeviation协方差covariance 相对标准差(标准差/平均值)coefficientofvariation计数(非缺失) count 计缺失数countmissing 中位数median范围range数据求和sumdata众数mode 歪斜度skewness(三阶中心矩/σ^3) 曲率度kurtosis(四阶中心矩/σ^4) 几何平均值geometricmean r次均方moment线性相关数linearcorrelation 平均绝对偏差meandeviation和谐平均值harmonicmea(n/Σ1/xi) 二次平均值quadraticmean查找百分位数percentile 查找分数位数据quantile 查找四分位数quartile查找十分位数decile > with(stats): > a1:=[3,4,5];a2:=[31,32,33]; >describe[mean](a1),describe[mean](a2); >describe[standarddeviation](a1);describe[standarddeviation](a2);#标准差相同 >describe[coefficientofvariation](data1): evalf("); #相对标准差大 >describe[coefficientofvariation](data2): evalf("); #相对标准差小 >describe[sumdata](a2); >describe[mode]([1,2,5,3,7,3,2,3,6,3,4]);#众数为3 > 第二节 拟合回归分析
有函数 fit[leastsquare]最小二乘法 fit[leastmediansquare] 最小中间二乘法 格式fit[leastsquare[[变量],回归方程,{系数}]]([[第一个变量数据],[第二个],……]) 回归方程及系数省略,为线性回归;变量数据写成点的坐标,输出结果将是错误的。
with(stats): > fit[leastsquare[[x,y,z]]]([[1,2,3,5],[2,4,6,8],[3,5,7,10]]); > fit[leastsquare[[x,y], y=a*x^2+b*x+c, {a,b,c}]]([[1,2,3,4,5],[2,3,4,5,7]]); > with(stats): > fit[leastsquare[[x,y,z,s]]]([[1,2,3,5,6],[1,2,4,6,8],[3,5,7,8,10],[9,5,3,2,0]]); >with(stats):fit[leastsquare[[x,y],y=a+b*x+c*x^2+d*x^3,{a,b,c,d}]]([[1,2,3,4,5,6,7,8,9,10],[4,7,13,20,30,38,42,45,47,49]]); >LOT(POINTS([1,4],[2,7],[3,13],[4,20],[5,30],[6,38],[7,42],[8,45],[9,47],[10,49]),SYMBOL(DIAMOND),COLOUR(RGB,1,0,1));plot(-46.93806744+45.05867515*x^(1/3),x=1..10, linestyle=20, color=green);
第三节 数据形式变换transform
格式 可用函数apply classmark cumulativefrequency deletemissing divideby frequency moving multiapply scaleweight split standardscore statsort statvalue subtractfrom tally tallyinto
第四节 按分布产生随机数random
格式 1. random[分布[参数]](n) 2. f:=random[分布[参数]]('generator[m]');定义该分布的随机数发生器,用'f()'$n产生所需随机数,n为产生随机数个数。 可用概率分布有: (离散型) 二项分布binomiald[n,p]均匀分布discreteuniform[a,b] 经验分布empirical[list_prob] 超几何hypergeometric[N1,N2,n] 反二项分布negativebinomial[n,p] 泊松分布poisson[mu] (连续型) 均匀uniform[a,b] 指数exponential[alpha,a] 正态normald[mu,sigma] 卡方chisquare[n] t-分布studentst[n] F-分布fratio[n1,n2] 伽马gamma[a,b] 拉普拉斯laplaced[a,b] 罗吉斯特logistic[a,b] 对数正态lognormal[mu,sigma] 贝塔beta[nu1,nu2] 柯西cauchy[a,b] 威布尔分布weibull[a,b]
> with(stats):p:=random[poisson[15]]('generator[2]'):#建立参数为15的泊松分布随机数发生器p > 'p()'$10;#用上面的随机数发生器产生10个随机数 > random[normald[30,2]](10);#生成10个正态分布随机数
第五节 分布的数值计算statevalf
有函数:cdf分布函数, icdf分布函数反函数, pdf概率密度函数, dcdf离散型分布函数idcdf离散型分布函数反函数,pf离散型概率函数 格式: statevalf[函数,分布[参数]](点或概率值)
> with(stats): > statevalf[cdf,normald](4);#标准正态分布在4点函数值 > statevalf[icdf,chisquare[14]](0.9);#卡方分布0.1临界值 > statevalf[icdf,normald[1,2]](.9);#N(1,2)变量概率为0.9的点 > alias(t=statevalf[icdf,studentst[12]]):t(0.95);#t-分布用别名alias中t代替对应命令,求0.05临界值 > statevalf[icdf,fratio[2,10]](0.95);#F-分布0.05临界值
第六节 统计绘图statplots
函数有:boxplot,histogram,notchedbox, quantile, quantil2, scatter1d, scatter2d, symmetry, xscale,xshift,xyexchange
> with(stats): > Xdata := [4.535, 4.029, 5.407, 1.605, 5.757, 3.527, 7.890, 8.159, 6.092, > 13.442, 2.845, 5.172, 3.277, 8.810, 3.657, 7.226, 3.851, 2.162, > 2.668, 4.692]: > Ydata:= [7.454, 4.476, 2.873, 5.476, 9.975,-1.476, 1.033, 1.140, 4.813, > .450, -.788, 9.389, 4.811,-3.107, 4.407, 5.534, 1.691, -.789, > 1.684, 1.605]: > plots[display](statplots[scatter2d](Xdata,Ydata), view =[0..17,-4..14], axes=FRAME);#散点图 > plots[display](statplots[boxplot[15]](Ydata),view =[0..17,-4..14], axes=FRAME);#框式图在右 > plots[display](statplots[xyexchange](statplots[notchedbox[12]](Xdata)),view =[0..17,-4..14], axes=FRAME);#刻痕框图在上 > plots[display](statplots[notchedbox](Xdata)); > plots[display](statplots[histogram](Xdata));#直方图
第七节 方差分析anova 仅有一个函数oneway,故直接用with(stats[anova])调入软件包
> with(stats[anova]):with(stats[describe]): > a1:=[10,11,8]:a2:=[Weight(9,3),11]:a3:=[missing,10,11,7,12]:data:=[a1,a2,a3]; > oneway(data); > a1:=[1,2,3,4,5]:a2:=[3,5,2,6,7]:a3:=[4,0,8,-1,7]: > data:=[a1,a2,a3]neway(data); >eval([a3]);
注:输出结果[[a1,b1,c1][a2,b2,c2][a3,b3]][a1,a2,c1/c2,d]中a3=a1+a2为总自由度,a1为组自由度;b3=b1+b2为总方差,b1为组间方差;ci=bi/ai;d为自由度是a1,a2的F-分布不超过c1/c2的概率。
|