basic r codes

4
A)For histogram with normal curve x <- gmat > h<-hist(x, breaks=10, col="blue", xlab="Gmat score", + main="Histogram with Normal Curve") > xfit<-seq(min(x),max(x),length=40) > yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) > yfit <- yfit*diff(h$mids[1:2])*length(x) > lines(xfit, yfit, col="red", lwd=2) b)Setwd (“Directory path”)command , use header =true if the data has to be taken directly c)To read a file house<-read.csv(file.choose(),header=T,sep=",") to attach attach(variable) d) graphs https://gossetsstudent.wordpress.com/2010/08/02/159/ e) to remove file rm(list=ls()) hist(Price,labels=T) > sd(Price) > hom<-read.csv(file.choose(),header=T,sep=",") > attach(hom) f) For histogram and box plot see favourites in chrome g) for getting package: install.packages (Write package name here) h) for loading a package : library(Write package name here) i) boxplot : boxplot(Living.Area,horizontal=T,main="Boxplot of Living Area",labels=T) for quantile use

Upload: srekha11

Post on 10-Dec-2015

1 views

Category:

Documents


0 download

DESCRIPTION

Contains R codes for basic understanding. This includes creating histogram , working directory .Some plots are also mentioned

TRANSCRIPT

Page 1: Basic r Codes

A)For histogram with normal curve

x <- gmat> h<-hist(x, breaks=10, col="blue", xlab="Gmat score", + main="Histogram with Normal Curve") > xfit<-seq(min(x),max(x),length=40) > yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) > yfit <- yfit*diff(h$mids[1:2])*length(x) > lines(xfit, yfit, col="red", lwd=2)

b)Setwd (“Directory path”)command , use header =true if the data has to be taken directly

c)To read a file

house<-read.csv(file.choose(),header=T,sep=",")

to attach

attach(variable)

d) graphs

https://gossetsstudent.wordpress.com/2010/08/02/159/

e) to remove file

rm(list=ls())

hist(Price,labels=T)> sd(Price)

> hom<-read.csv(file.choose(),header=T,sep=",")> attach(hom)

f) For histogram and box plot see favourites in chrome

g) for getting package: install.packages (“Write package name here”)

h) for loading a package : library(Write package name here)

i) boxplot : boxplot(Living.Area,horizontal=T,main="Boxplot of Living Area",labels=T)

for quantile use

quantile(Price/1000,c(0,0.25,0.5,0.75,1))

j) histogram : hist(Living.Area,labels=T,ylim=c(0,400),main="Histogram of Living Area")

h) Confidence interval for proportion (m) and sample size n using R for 95% confidence level change the 0.975 according to confidence level required.

Page 2: Basic r Codes

m + qnorm (0.975, 0, 1)*sqrt ((m*(1-m)/n))

i) For tossing of coin we use binomial , here use he following formulae to compute the p valuepbinom(value, size , probability of individual success) . Note then you have to multiply by 2 for two tailed test.

example. pbinom(8,25,0.5) see page 314

Note to calculate the test statistic i.e the z value or the t value as is not the probability the formula if the population mean, sample mean and only sample standard deviation is known is

Z or t = (sample mean – population mean)/ s/sqrt(n)

j) p value of a proportion = first calculate z value by below formula and from z value compute p value using formulae pnorm(z value)

Hypothesis

1) If alternative is greater then it is a right tailed test or if alternative is slow then it is a left tail test

By default the lower.tail is true

t= (1950-1800)/(600/sqrt(36))

t

pt(t,35,lower.tail=FALSE)

2) T test and Prop test

By default the alternative is not equalFor left tail alternative is lessprop.test(181,1000,p=0.2,alternative='less')

Regression using R.

a) Reading data : ceosal2<-read.csv(file.choose(),header=T,sep=",")b) Attach data : attach(ceosal2)c) Plotting : plot(Salr~MarCap,data=ceosal2)d) Linear Regression working through : ouput2=lm(Salr~MarCap,data=ceosal2)e) Linear Regression output through : summary(ouput2)

s=0

ga<-vector("list")

Page 3: Basic r Codes

for (i in 0:3)

{

s=(s+i)

gaa[i]<-s

print(s)

}

xa =0

for (i in 1:16)

{

xa = xa+demand3[i]

print(xa)

}

x=0

for (i in 1:2)

{x=x+1

print(x)

}

xa=1

xb=2

xc<-append(xa,xb)

xc

xc[1]

xc[2]

# coverting categorical variable to numeric

> iris$catg<-as.factor(iris$Species)

> iris$catg<-as.numeric(iris$catg)

Page 4: Basic r Codes