统计与建模R语言基本操作

Posted osea

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了统计与建模R语言基本操作相关的知识,希望对你有一定的参考价值。

  1 # vec <- rep( seq(1,5,by=0.5),3)
  2 
  3 # vec <- seq( 1 , 10 , by = 1 )
  4 # min(vec)  #最小值
  5 # max(vec)  #最大值
  6 # range(vec)  #范围
  7 # length(vec) #长度
  8 # sum(vec)  #总和
  9 # prod(vec) #向量元素所有的乘积
 10 # median(vec) #中位数
 11 # mean(vec) #均值
 12 # var(vec) #样本方差
 13 # sort(vec) #排序
 14 # order(-vec) #排序 的是 索引并不改变向量的实际位置
 15 # vec
 16 
 17 # vec <- 1:5
 18 # Logic_vec <- vec > 3 
 19 # Logic_vec
 20 
 21 # str = c( "x" , "y" , "z" , "w" )
 22 # paste("result.",str,sep="")
 23 
 24 # x <- -5 : 5
 25 # y <- numeric( length(x) )
 26 # y[x<0] <- 1 - x[x<0]
 27 # y[x>=0] <- 1 + x[x>=0]
 28 # x;y
 29 
 30 # sex <- c("M","F","M","M","F");sex
 31 
 32 # sexf <- factor(sex) ; sexf 
 33 
 34 # sex.level <- levels(sexf) ; sex.level
 35 
 36 # sex.tab <- table( sexf ) ; sex.tab 
 37 
 38 # sex <- c("M","F","M","M","F")
 39 # sexf <- factor(sex);
 40 # sex.level <- levels(sexf);
 41 # height <- c(174,165,180,171,160)
 42 # tapply( height , sex , mean )
 43 
 44 
 45 # A <- matrix( 1:9 , nrow = 3 , ncol = 3 , byrow = TRUE ) ; 
 46 # A[3,3] = 10;A;
 47 # B <- matrix( 1 , nrow = 3 , ncol = 1 , byrow = TRUE ) ; B 
 48 
 49 # A <- matrix( 1:9 , nrow = 3 , ncol = 3 , byrow = TRUE ) ; 
 50 # A[3,3] = 10;
 51 # det(A)
 52 
 53 # A <- t( array( c(1:8,10) , dim=c(3,3) ) );A
 54 # Inv_A <- solve(A) ; Inv_A
 55 
 56 # A <- t( array( c(1:8,10) , dim=c(3,3) ) );A
 57 # Inv_A <- solve(A) ; Inv_A
 58 # E = A %*% Inv_A ; E 
 59 
 60 # A <- t( array( c(1:8,10) , dim = c(3,3) ) );A
 61 # b <- c( 1 , 1 , 1 );b
 62 # x <- solve( A,b ) ; x 
 63  
 64 # A <- t( array( c(1:8,10) , dim = c(3,3) ) );A
 65 # Sm <- tcrossprod (A,A); Sm
 66 
 67 
 68 # ev <- eigen(Sm) ; ev
 69 
 70 # A <- t( array( c(1:8,10) , dim = c(3,3) ) );A
 71 # svdA <- svd(A) ; svdA 
 72 # u = svdA$u;u
 73 # v = svdA$v;v
 74 # d = svdA$d;d
 75 # u %*% diag(d)  %*% t(v)
 76 
 77 # A <- t( array( c(1:8,10) , dim = c(3,3) ) );A
 78 # apply( A , 2 , sum )
 79 
 80 
 81 # fzero <- function( f , a , b , eps = 1e-5 )
 82 #     if( f(a) * f(b) > 0 )
 83 #         list( fail = "Unfound ")
 84 #     else
 85 #         repeat
 86 #             if( abs(b-a) < eps ) break 
 87 #             x <- (a+b) / 2 
 88 #             if( f(a) * f(x) < 0 )
 89 #                 b <- x 
 90 #             
 91 #             else
 92 #                 a <- x
 93 #             
 94 #         
 95 #         list(root = (a+b)/2,fun = f(x) )
 96 #     
 97 # 
 98 # 
 99 # f <- function( x )  x^3 - x - 1 
100 # fzero(f,0,10,1e-6)
101 
102 # area <- function( f , a , b , eps = 1e-6 , lim = 10 )
103 #     fun1 <- function( f , a , b , fa , fb , a0 , eps , lim , fun )
104 #         d <- ( a+b ) / 2 ;
105 #         h <- ( b-a ) / 4 ;
106 #         fd <- f(d) ;
107 #         a1 <- h * ( fa+fd )
108 #         a2 <- h * ( fd+fb )
109 #         if( abs(a0-a1-a2) < eps || lim == 0 )
110 #             return ( a1 + a2 )
111 #         else
112 #             return ( fun(f,a,d,fa,fd,a1,eps,lim-1,fun)
113 #                     +fun(f,d,b,fd,fb,a2,eps,lim-1,fun))
114 #         
115 #     
116 #     fa <- f(a);
117 #     fb <- f(b);
118 #     a0 <- ((fa+fb) * (b-a))/2;
119 #     fun1(f,a,b,fa,fb,a0,eps,lim,fun1)
120 # 
121 # 
122 # f <- function(x) x
123 # quad <- area(f,0,10) ; quad 

 

以上是关于统计与建模R语言基本操作的主要内容,如果未能解决你的问题,请参考以下文章

《R语言入门与科学文献绘图》网络培训班开始报名

Titanic生存预测(Kaggle入门赛)——基于R语言

r语言需要下载啥软件

R语言统计分析应用与SASSPSS的比较

什么是r语言

R语言书籍推荐