Tuesday, June 22, 2010

Statistics R software examples

1) Plot Chi-Squared Distribution:
>help(Chisquare)
> x <- seq(-20,20,by=.2) > y <- dchisq(x,df=10) # df is degree distribution > plot(x,y)
2) Vector
>
y <- 1:10 #create a vector of consecutive integers from 1 to 10
> y
<-c(4,9,2,8) #create a vector with 4 components > sum(y) #sum of elements in y
> cumsum(y) #cumulative sum vector
> sort(y) # increase order
> sort(y, decreasing=T) # decreasing order
> length(y) # number of elements in y
> y[2] # the second element of y
>y[1:2] # the first two elements in y
3) function
find root
> y.fun<-function (x)
{y<-(log(x))^2-x*exp(-x^3)
}
>root.fun<- function ()
{ x<-seq(0.2,2,0.01)
y<-y.fun(x)
plot(x,y,type="l")
abline(h=0)
r1 <- uniroot(y.fun,lower=0.2,upper=1)$root
r2 <- uniroot(y.fun,lower=1,upper=2)$root
cat("Roots : ", round(r1,4), " ", round(r2,4),"\n")
}
> root.fun()
4) Basic plots:
> my <- read.csv(file="my.dat",sep=",",head=TRUE) # read in my.dat


> stripchart(my$vals)
> stripchart(my$vals,method="jitter")
> stripchart(my$vals,vertical=TRUE)
> stripchart(my$vals,method="stack",
+ main='test',
+ xlab='number') # strip chart
>hist(my$vals,breaks=2) # number of division in x is 2
>boxplot(my) #box plot

No comments:

Post a Comment