# ================================================== # Homework R: 1 # ================================================== # A x <- c(8,10,14,3,9,12,12,16,16,6,11,15,28,22,16,7,9,14,5,10,11,13,17,15,5,12,14,17,21,18) y <- c(5,7,8,5,7,10,9,17,15,9,11,15,18,25,10,5,7,8,5,8,10,9,18,14,9,10,16,18,25,10) mean(x) median(x) var(x) sd(x) mean(y) median(y) var(y) sd(y) sum(x^2)-(sum(x))^2/length(x) sum(y^2)-(sum(y))^2/length(y) sum(x*y)-(sum(x)*sum(y))/length(x) boxplot(x) boxplot(y) cor(x,y) lm(y~x) plot(x,y) abline(lm(y~x)) # B mydata <- read.csv("c:/Documents and Settings/username/Desktop/soc510hw1.csv") attach(mydata) names(mydata) mean(age) mean(wage) mean(educ) sd(age) sd(wage) sd(educ) bins=seq(0, 60, by=2) hist(wage, breaks=bins, col="gray", freq=FALSE) x <- rnorm(1000,mean(wage),sd(wage)) curve(dnorm(x, mean=mean(wage), sd=sd(wage)), col="darkgreen", add=TRUE) abline(v=mean(wage), col="blue", lty=2) abline(v=median(wage), col="red", lty=2) boxplot(age) boxplot(wage) boxplot(educ) cor(wage, age) cor(wage, educ) lm(wage~age) lm(wage~educ) plot(educ, wage) abline(lm(wage~educ)) # C pnorm(1.21532) - pnorm(-.83235) 1-pnorm(2.5321) pnorm(-1.6523583) 1-pnorm(3.65, mean=3.21, sd=.28) 1-pnorm(3.65, mean=3.21, sd=.28/sqrt(50)) # -------------------------------------------------------------- # additional examples: histogram with a normal distribution line # -------------------------------------------------------------- hist(wage, freq=FALSE) x <- rnorm(1000,mean(wage),sd(wage)) curve(dnorm(x, mean=mean(wage), sd=sd(wage)), col="red", add=TRUE) hist(wage, nclass=30, freq=FALSE, col="grey") x <- rnorm(1000,mean(wage),sd(wage)) curve(dnorm(x, mean=mean(wage), sd=sd(wage)), col="red", add=TRUE) abline(v=mean(wage), col="blue", ) abline(v=median(wage), col="darkgreen", lty=2) # -------------------------------------------------------------- # editing your data with an data window # -------------------------------------------------------------- mydata <- edit(as.data.frame(NULL)) attach(mydata) names(mydata) # if you find an error, you can edit your data by doing edit(mydata) # -------------------------------------------------------------- # graph of the standard normal distribution # -------------------------------------------------------------- x <- rnorm(1000,0,1) curve(dnorm(x, mean=0, sd=1), from=-4, to=4) # -------------------------------------------------------------- # to calculate area of a normal distribution # -------------------------------------------------------------- install.packages('PASWR') library(PASWR) normarea(-1.96, 1.96, 0, 1)