多元统计分析及R语言建模#学习笔记

您所在的位置:网站首页 多元统计分析及r语言建模论文 多元统计分析及R语言建模#学习笔记

多元统计分析及R语言建模#学习笔记

2023-08-23 05:13| 来源: 网络整理| 查看: 265

最近在学习R语言,把书上的代码都敲一遍,仅供学习

函数c()创建向量

x1=c(171,175,159,155,152,158,154,164,168,166,159,164) x2=c(57,64,41,38,35,44,41,51,57,49,47,46) length(x1) length(x2)

12  12

查看数据类型

mode(x1)

'numeric'

生成等差数列向量

a=1:12 123456789101112 b=c(1,3,6:4,9) 136549

创建矩阵

A=matrix(c(1,4,2,5,3,6),nrow=2,ncol=3) A A matrix: 2 × 3 of type dbl 123456 B=matrix(c(1,2,3,4,5,6),3,2) #3行2列 B A matrix: 3 × 2 of type dbl 142536 matrix(x1,nrow=3,ncol=4) #按列摆放顺序 A matrix: 3 × 4 of type dbl 171155154166175152164159159158168164 matrix(x1,nrow=4,ncol=3) A matrix: 4 × 3 of type dbl 171152168175158166159154159155164164 matrix(x1,nrow=4,ncol=3,byrow=T) #按行排列的矩阵 A matrix: 4 × 3 of type dbl 171175159155152158154164168166159164

矩阵转置

t(A) #A'为转置矩阵,矩阵A并未发生改变 A matrix: 3 × 2 of type dbl 142536

矩阵加减

A[,1:2]+B[1:2,] A matrix: 2 × 2 of type dbl 26610 A[,2:3]-B[2:3,] A matrix: 2 × 2 of type dbl 0-220

矩阵相乘

C=A%*%B C A matrix: 2 × 2 of type dbl 14323277 D=B%*%A D A matrix: 3 × 3 of type dbl 172227222936273645

矩阵对角运算

diag(D) #取方阵的对角元素 172945 diag(C) 1477 I=diag(3) #对一个正整数k应用diag()函数将产生k维单位矩阵 I A matrix: 3 × 3 of type dbl 100010001 c=c(1,2,3,4) #对一个向量用diag()函数将产生以这个向量为对角元素的对角矩阵 M=diag(c) M A matrix: 4 × 4 of type dbl 1000020000300004

矩阵求逆

solve(C) A matrix: 2 × 2 of type dbl 1.4259259-0.5925926-0.59259260.2592593

矩阵的特征值与向量

D.e=eigen(D,symmetric=T) D.e eigen() decomposition $values [1] 9.040267e+01 5.973275e-01 1.329470e-15 $vectors [,1] [,2] [,3] [1,] -0.4286671 0.8059639 0.4082483 [2,] -0.5663069 0.1123824 -0.8164966 [3,] -0.7039467 -0.5811991 0.4082483 D.e$vectors%*%diag(D.e$values)%*%t(D.e$vectors) #D=URU',R为D的特征值组成的对角矩阵 A matrix: 3 × 3 of type dbl 172227222936273645

矩阵的奇异值分解

(A=matrix(1:18,3,6)) A matrix: 3 × 6 of type int 147101316258111417369121518 (A.s=svd(A)) #矩阵的奇异值分解

$d

45.89453220272511.64070530353061.36652174299492e-15

$u

A matrix: 3 × 3 of type dbl -0.52903540.743945510.4082483-0.57607150.03840487-0.8164966-0.6231077-0.667135770.4082483

$v

A matrix: 6 × 3 of type dbl -0.07736219-0.71960032-0.4076688-0.19033085-0.508932470.5745647-0.30329950-0.29826463-0.0280114-0.41626816-0.087596790.2226621-0.529236820.12307105-0.6212052-0.642205480.333738890.2596585 A.s$u%*%diag(A.s$d)%*%t(A.s$v) #'$'表示从一个dataframe取出某一列数据 A matrix: 3 × 6 of type dbl 147101316258111417369121518

矩阵的维数

A=matrix(1:16,4,4) dim(A) 44 nrow(A)

4

ncol(A)

4

矩阵的和

A sum(A) A matrix: 4 × 4 of type int 15913261014371115481216

136

rowSums(A) #矩阵按行求和 28323640 colSums(A) #矩阵按列求和 10264258

矩阵的均值

mean(A) rowMeans(A) #按行求均值 colMeans(A) #按列求均值

8.5

78910 2.56.510.514.5

数据框的构成

X=data.frame(x1,x2) #x1,x2的长度必须一致 X A data.frame: 12 × 2 x1x2171571756415941155381523515844154411645116857166491594716446 Y=data.frame('身高'=x1,'体重'=x2) Y A data.frame: 12 × 2 身高体重171571756415941155381523515844154411645116857166491594716446

数据框的组成

rbind(x1,x2) #按行合并 A matrix: 2 × 12 of type dbl x1171175159155152158154164168166159164x2576441383544415157494746 cbind(x1,x2) #按列合并 A matrix: 12 × 2 of type dbl x1x2171571756415941155381523515844154411645116857166491594716446

按行显示

head(X) #前6行 A data.frame: 6 × 2 x1x2117157217564315941415538515235615844 tail(X) #后6行 A data.frame: 6 × 2 x1x2715441816451916857101664911159471216446

数据框的应用

apply(x,margin,fun)

x:数据框或矩阵

margin:指定对行还是对列运算(margin=1表示对行运算,margin=2表示对列运算)

fun:指定运算函数

Xr=apply(X,1,sum) #按行求和=rowSums Xr 228239200193187202195215225215206210 Xc=apply(X,2,sum) #按列求和=colSums Xc

x1 1945

x2 570

cbind(X,'行合计'=Xr) A data.frame: 12 × 3 x1x2行合计171572281756423915941200155381931523518715844202154411951645121516857225166492151594720616446210 rbind(X,'列合计'=Xc) A data.frame: 13 × 2 x1x21715717564159411553815235158441544116451168571664915947164461945570


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3