matrix在R语言中怎么用_matrix r语言
大家好,今天我想和大家分享一下我在“matrix在R语言中怎么用”方面的经验。为了让大家更好地理解这个问题,我将相关资料进行了整理,现在就让我们一起来学习吧。
1.R语言基本数据分析
2.R语言中的几种数据结构
3.r语言矩阵中byrow=false什么意思
R语言基本数据分析
R语言基本数据分析
本文基于R语言进行基本数据统计分析,包括基本作图,线性拟合,逻辑回归,bootstrap采样和Anova方差分析的实现及应用。
不多说,直接上代码,代码中有注释。
1. 基本作图(盒图,qq图)
#basic plot
boxplot(x)
qqplot(x,y)
2. 线性拟合
#linear regression
n = 10
x1 = rnorm(n)#variable 1
x2 = rnorm(n)#variable 2
y = rnorm(n)*3
mod = lm(y~x1+x2)
model.matrix(mod) #erect the matrix of mod
plot(mod) #plot residual and fitted of the solution, Q-Q plot and cook distance
summary(mod) #get the statistic information of the model
hatvalues(mod) #very important, for abnormal sample detection
3. 逻辑回归
#logistic regression
x <- c(0, 1, 2, 3, 4, 5)
y <- c(0, 9, 21, 47, 60, 63) # the number of successes
n <- 70 #the number of trails
z <- n - y #the number of failures
b <- cbind(y, z) # column bind
fitx <- glm(b~x,family = binomial) # a particular type of generalized linear model
print(fitx)
plot(x,y,xlim=c(0,5),ylim=c(0,65)) #plot the points (x,y)
beta0 <- fitx$coef[1]
beta1 <- fitx$coef[2]
fn <- function(x) n*exp(beta0+beta1*x)/(1+exp(beta0+beta1*x))
par(new=T)
curve(fn,0,5,ylim=c(0,60)) # plot the logistic regression curve
3. Bootstrap采样
# bootstrap
# Application: 随机采样,获取最大eigenvalue占所有eigenvalue和之比,并画图显示distribution
dat = matrix(rnorm(100*5),100,5)
no.samples = 200 #sample 200 times
# theta = matrix(rep(0,no.samples*5),no.samples,5)
theta =rep(0,no.samples*5);
for (i in 1:no.samples)
{
j = sample(1:100,100,replace = TRUE)#get 100 samples each time
datrnd = dat[j,]; #select one row each time
lambda = princomp(datrnd)$sdev^2; #get eigenvalues
# theta[i,] = lambda;
theta[i] = lambda[1]/sum(lambda); #plot the ratio of the biggest eigenvalue
}
# hist(theta[1,]) #plot the histogram of the first(biggest) eigenvalue
hist(theta); #plot the percentage distribution of the biggest eigenvalue
sd(theta)#standard deviation of theta
#上面注释掉的语句,可以全部去掉注释并将其下一条语句注释掉,完成画最大eigenvalue分布的功能
4. ANOVA方差分析
#Application:判断一个自变量是否有影响 (假设我们喂3种维他命给3头猪,想看喂维他命有没有用)
#
y = rnorm(9); #weight gain by pig(Yij, i is the treatment, j is the pig_id), 一般由用户自行输入
#y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)
Treatment <- factor(c(1,2,3,1,2,3,1,2,3)) #each {1,2,3} is a group
mod = lm(y~Treatment) #linear regression
print(anova(mod))
#解释:Df(degree of freedom)
#Sum Sq: deviance (within groups, and residuals) 总偏差和
# Mean Sq: variance (within groups, and residuals) 平均方差和
# compare the contribution given by Treatment and Residual
#F value: Mean Sq(Treatment)/Mean Sq(Residuals)
#Pr(>F): p-value. 根据p-value决定是否接受Hypothesis H0:多个样本总体均数相等(检验水准为0.05)
qqnorm(mod$residual) #plot the residual approximated by mod
#如果qqnorm of residual像一条直线,说明residual符合正态分布,也就是说Treatment带来的contribution很小,也就是说Treatment无法带来收益(多喂维他命少喂维他命没区别)
如下面两图分别是
(左)用 y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)和
(右)y = rnorm(9);
的结果。可见如果给定猪吃维他命2后体重特别突出的数据结果后,qq图种residual不在是一条直线,换句话说residual不再符合正态分布,i.e., 维他命对猪的体重有影响。
R语言中的几种数据结构
1. 向量 Vector
向量是用于储存数值型、字符型或逻辑型数据的一维数组。执行组合功能能的函数 可用来创建向量。
单个向量中的数据必须拥有相同的类型或模式(即数值型、字符型或逻辑型)。同一向量中无法混杂不同模式的数据。
2. 矩阵 Matrix
矩阵是一个二维数组,知识每个元素都拥有相同的模式(数值型、字符型或逻辑型)。可通过函数 创建矩阵。
3. 数组 Array
数组与矩阵类似,但是维度可以大于2。数组可通过 函数创建。
数组是矩阵的一个自然推广。它们在编写新的统计方法时很有用。像矩阵一样,数组中的数据也只能拥有一种模式。从数组中选取元素的方法与矩阵相同。
4. 数据框 Data Frame
这是R语言最常用的数据类型。不同的列可以包含不同模式的数据。每一列数据的模式必须相同,且必须等长。数据框可通过函数 创建。
5. 列表 List
列表是一些对象(或成分)的有序集合。列表允许你整合若干(可能无关的)对象到单个对象名下。例如,某个列表中可能是若干向量、矩阵、数据框,甚至其他列表的组合。可以使用函数 创建列表。
许多R的运行结果都是以列表的形式返回的。需要取出其中哪些成分由分析人员决定。
r语言矩阵中byrow=false什么意思
R语言中的几种数据结构
一 R中对象的5种基本类型
字符(character)
整数 (integer)
复数(complex)
逻辑(logical:True/False)
数值(numeric:real numbers)
查看对象类型的命令:class(x)
二 R语言中有如下几种数据结构:
向量 vector() 组内元素必须类型一致,否则将会被强制转换。
(1) 创建向量的三种方式:
<span style="font-size:18px;">x <- vector("numeric", length = 10)
x <- 1:4
x <- c("a",12,TRUE)</span>
(2) 强制转换的几个函数:
as.numeric(x) / as.character(x) / as.logical(x)
矩阵 matrix() 一列一列的填充元素
按行合并:rbind() 按列合并:cbind()
数组 array() 可以有多个维度
列表 list() 可以包含不同类型的元素
因子 factor()
(1) 分类数据/有序 vs. 无序
(2) 整数向量+标签(label)(优于整数向量)
Male/Female vs. 1/2
常用于lm(),glm()
(3) levels设置基线水平
table() 查看因子信息 unclass() 去除因子属性日期
x <- Sys.Date() 得到系统当前日期
n(x) x距离1970-01-01的天数
时间 POSIXct / POSIXlt
POSIXct:整数,常用于存入数据框 as.POSIXct()
POSIXlt:列表,还包含星期、年月日等信息。as.POSIXlt()
strptime(x, format = "...") 将一般格式转化为时间格式
在R语言中,可以通过参数byrow使得matrix,以列优先的次序转换成矩阵。R语言是默认byrow=False,即数据按列输入,byrow=True则按行输入,如同是上题,若byrow=FALSE,则输出结果为:
m<-matrix(n[1:3],n[2:4],n[3:5],n[4:6],n[5:7],n[6:8],n[7:9],n[8:10],nrow=3)而且matrix(1:6,2,3,byrow=TRUE)可简写为matrix(1:6,2,3,T)。
所有R的函数和数据集是
保存在程序包里面的。只有当一个包被载入时,它的内容才可以被访问。一些常用、基本的程序包已经被收入了标准安装文件中,随着新的统计分析方法的出现,标准安装文件中所包含的程序包也随着版本的更新而不断变化。
在另外版安装文件中,已经包含的程序包有:base—R的基础模块、mle—极大似然估计模块、ts—时间序列分析模块、mva—多元统计分析模块、survival—生存分析模块等等。
以上内容参考:百度百科-R语言
好了,今天关于“matrix在R语言中怎么用”的话题就讲到这里了。希望大家能够对“matrix在R语言中怎么用”有更深入的认识,并且从我的回答中得到一些帮助。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。