티스토리 툴바

달력

012012  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  •  
  •  
  •  
  •  

 1#

cos=matrix(c(24,32,45,20,35,60,50,20,15),nr=3)

rownames(cos)=c("안하겠다","고려해보겠다","하겠다")

colnames(cos)=c("2o","30","40")

addmargins(cos)

rowSums(cos)

colSums(cos)

chi=chisq.test(cos)

 

#2

machinea=c(501,502,495,498,499,506)

machineb=c(508,510,503,504,500,504,505)

분산이 같은가에대한f검정실시

var.test(machinea,machineb)

 

t.test(machinea,machineb,var.equal=TRUE,alternative="two.sided")

 

boxplot(machinea,machineb)

 

#3

xxx=c(1,2,3,5,7,9,8,10)

yyy=c(5,9,10,12,15,13,17,18)

out=lm(yyy~xxx)

summary(out)

plot(xxx,yyy)

 

#4

ou1=rnorm(10,0,2)

hist(ou1)

qqnorm(ou1)

qqline(ou1)

 

#5

qnorm(0.55,0,2)

qnorm(0.45,0,2,lower.tail=FALSE)

pt(11,7)-pt(-11,7)

#6

gx=seq(0,5,0.1)

gy=seq(-1,4,0.1)

f=function(gx,gy){gx^2+gy^2+(gx-2)*(gy-3)}

outt=outer(gx,gy,FUN=f)

persp(gx,gy,outt)

contour(gx,gy,outt)

#7

my_reg<-function(x,y){

beta1=sum(   (x-mean(x))*(y-mean(y))/sum((x-mean(x))^2)   )

beta0=mean(y)-beta1*mean(x)

return(list(beta0,beta1))

}

my_reg(xxx,yyy)

####

my_summary<-function(n,dist){

  if(dist==1) x=rnorm(n,0,1)

  if(dist==2) x=rexp(n,2)

temp.m<-mean(x)

temp.s<-sd(x)

temp.v<-var(x)

temp.r<-range(x)

temp.max<-max(x)

temp.min<-min(x)

temp.q<-quantile(x,c(0.1,0.2,0.25,0.5,0.75,0.8,0.9))

return(list(temp.m,temp.s,temp.v,temp.r,temp.max,temp.min,temp.q))

}

e=my_summary(20,20)

###

x=c(159,208,101,121,224,222,379,179,250,170)

 

my_t_test=function(x,mu0,side){

  n=length(x)

  dfn=n-1

  muhat=mean(x)

  shat=sd(x)

  t=(muhat-mu0)/(shat/sqrt(n))

   if(side==1){p_value=2*(1-pt(abs(t),df=dfn))}

   if(side==2){p_value=1-pt(t, df=dfn)}

   if(side==3){p_value=pt(t, df=dfn)}

 

    cl_upper=muhat+qt(1-alpha/2,df=n-1)*(shat/sqrt(n))

    cl_lower=muhat-qt(alpha/2,df=n-1)*(shat/sqrt(n))

return(list(t,p_value,cl_lower,cl_upper))}

 

############################

#Random data generating function#

############################

data.gen<-function(dist,n){

if(dist==1){mu1<-0

           sigma<-1

           temp<-rnorm(n,mu1,sigma)}

if(dist==2){tdf<-5

           temp<-rnorm(n,mu1,sigma)}

temp.m<-mean(temp)

temp.v<-var(temp)

return(temp,temp.m,temp.v)}

z=data.gen(1,20)

##

my.cor<-function(x,y){

n=length(x)

tempx=0; tempy=0; tempx2=0; tempy2=0; tempxy=0

for(i in 1:n){

           tempx=tempx+x[i]

           tempy=tempy+y[i]

           tempx2=tempx2+x[i]^2

           tempy2=tempy2+y[i]^2

           tempxy=tempxy+x[i]*y[i]  }

xbar=tempx/n

ybar=tempy/n

num=tempxy-n*xbar*ybar

den=sqrt(tempx2-n*xbar^2)*sqrt(tempy2-n*ybar^2)

r=num/den

return(list(r))}

##통계함수

hm.summary<-function(x){

     n=length(x)

     temp=0; temp1=0

     for (i in 1:n){

        temp=temp+x[i]

        temp1=temp1+x[i]^2

     }

     m=temp/n

     var=(temp1-n*m^2)/(n-1)

     sd=sqrt(var)

     return(list(m,var,sd))

    }

   -)출력하기

x=c(1,2,3,4,5)

  hm.summary(x)

mean_x=hm.summary(x)[[1]]

var_x=hm.summary(x)[[2]]

sd_x=hm.summary(x)[[3]]

 

그래프함수

dist.graph<-function(x){

    par(mfrow=c(2,2))

    hist(x)

    stem(x)

    boxplot(x)

    plot(x)

    }

##랜덤데이터 발생함수

data.gen<-function(n,dist){

    if(dist==1) x=rnorm(n,0,1)

    if(dist==2) x=rexp(n,2)

    if(dist==3) x=rpois(n,3)

return(x)}

 

y=data.gen(10,1)

data.gen(100,2)

d=data.gen(100,3)

mean(d)

 

## x=c(8.30,9.50,9.60,8.75,8.40,9.10,8.15,8.80)

t.test(x,mu=8.5)

 

x1=c(1.1,2.3,4.3,2.2,5.3)

x2=c(2.3,4.3,3.5)

t.test(x1,x2, var.equal=TRUE,alternative="two.sided")

 

t.test(x1,x2, var.equal=FALSE)

t.test(x1,x2,var.equal=TRUE, alt="greater")

 

t.test(x1,x2,var.equal=TRUE, alt="less")

더 크다고말할수 있는 통계적 근거가없다.

ho를 기각할만한 통계적 근거가 없다.

 

dd=read.table("C:/Documents and Settings/DS/바탕 화면/method.csv",header=T)

t.test(x ~ method, var.equal=T,data=dd)

t.test(dd$x~dd$method,var.equal=T)

###박스그래프

x=c(147,158,131,142,180)

y=c(122,128,125,123,115)

plot(x,y)

di=c(1:5)

boxplot(x,y, names=c("husband", "wife"))

plot(x,y, pch=as.character(di))

###바그래프

x1=c(1,2,3,4)

y1=c(40,30,35,20)

n1=c(20,30,45,40)

 

x=rbind(y1,n1)

barplot(x,names=c(1,2,3,4) ,beside=TRUE, legend.text=T,main="참석비율")

y=rowSums(prop.table(x))

barplot(y,beside=TRUE, legend.text=T, col=c("yellow","green"))

pie(y)

####

 

blood1=zz[(zz$gender==1),]

bloodia=blood1[,c(1,4)]

mean(bloodia)

###

score1<-data.frame(

       surname=c("kim", "lee", "park", "oh", "yang", "min", "jung"),

       k_score=c(93,76,87,92,98,75,82))

 

score2<-data.frame(

       name=c("kim", "lee", "park", "oh", "yang", "min", "jung", "choi"),

       E_score=c(90, 94, 88, 75, 79, 87, 88, 90))

d=merge(score1,score2, by.x="surname", by.y="name", all=TRUE)

 

par(mfrow=c(2,1))

x=(0:20)*pi/10

y=cos(x)

ysin=sin(x)

plot(x,y)     ;text(3,0, "sin(x)")

plot(x,ysin)  ;text(5,0.5, "cos(x)")

 

그래프들##명령어

x=(0:20)*pi/10

y=cos(x) ; ysin=sin(x) ; ysin2=sin(x)^2

 

par(mfrow=c(1,2))

   yy=cbind(y, ysin, ysin2)

   matplot(x, yy, type="pll", pch="*")

   plot(x,y)

      lines(x, ysin, type="p",pch="*")

      lines(x, ysin2)

 

plot(x,y, type="p", main="graphics", sub="pratice")

plot(x,y, type="p", main="graphics", sub="pratice")

plot(x,y, type="l")

plot(x,y, type="b")

plot(x,y, type="p", pch=19, col="red")

 

op= par(mfrow=c(2,2))

 plot(x,y, type="b", main= "cosine graph", sub="type=b")

 plot(x,y, type="o", las=1, bty="u", sub="type=0")

 plot(x,y, type="h", bty="7", sub="type=h")

 plot(x,y, type="s", bty="n", sub="type=s")

par(op)

 

##

data(cars)

attach(cars)

mean(speed)

mean(dist)

 

par(mfrow=c(2,2))

 

plot(speed, dist, pch=1)

  abline(v=15.4)

plot(speed, dist, pch=2)

  abline(v=42.98)

plot(speed, dist, pch=3)

  abline(-17, 4)

plot(speed, dist, pch=8)

  abline(v=15.4)

 abline(v=42.98)

 

 

zzz=read.csv(file="C:/Documents and Settings/DS/바탕 화면/조현미/63p 예제3.csv",

 header=TRUE) ; attach(zzz)

win.graph()

plot(x=bodywt, y=brainwt, pch="*", xlab="Bodyweight",

    ylab="Brain weight", xlim=c(0,250), ylim=c(o,1400), sub="primates")

 

plot(x=bodywt, y=brainwt, type="n"), xlab="Bodyweight"

                                     ylab="Brainweight")

 

  points(bodywt[brainwt>=400],brainwt[brainwt>=400], pch=3)

  points(bodywt[brainwt<400],brainwt[brainwt<400], pch=1)

legend(120, 1000, c("brain weight<=400", "brain weight>400"),

     pch=c(3,1))

 

par(mfrow=c(1,2))

 plot(bodywt, brainwt, xlim=c(o,300))

 text(x=bodywt, y=brainwt, labels=animal)

 plot(bodywt, brainwt, xlim=c(o,300))

 text(x=bodywt, y=brainwt, labels=animal, adj=0)

시험대비겸 한학기 총정리해서 올려보아요~
필요한분 가져가세요~
R 어느정도 아신다하는 분, R깔려 있다는 시는 분 그냥 복사해서 붙여넣으시면 어떤 내용인지 아실겁니다.

Posted by hermit해달
TAG ,