Advertisement
arossmiller

Homework 2 AC

Feb 10th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.97 KB | None | 0 0
  1. ###5.1###
  2. plot(x,y)
  3. plot(factor, y)
  4. barplot(y)
  5.  
  6. ###5.2###
  7. data1 <- read.table("c:\\temp\\scatter1.txt",header=T) #CHANGE FILE#
  8. attach(data1)
  9. names(data1)
  10.  
  11. plot(x1,y1,col="red")
  12. plot(x1,y1,col="red",xlab="Explanatory variable",ylab="Response variable")
  13.  
  14. abline(lm(y1~x1))
  15.  
  16. data2 <- read.table("c:\\temp\\scatter2.txt",header=T) #CHANGE FILE#
  17. attach(data2)
  18. names(data2)
  19.  
  20. points(x2,y2,col="blue",pch=16)
  21.  
  22. abline(lm(y2~x2))
  23.  
  24. plot(c(x1,x2),c(y1,y2),xlab="Explanatory variable", ylab="Response variable",type="n")
  25. points(x1,y1,col="red")
  26. points(x2,y2,col="blue",pch=16)
  27.  
  28. abline(lm(y1~x1))
  29. abline(lm(y2~x2))
  30.  
  31. range(c(x1,x2))
  32. range(c(y1,y2))
  33.  
  34. plot(c(x1,x2),c(y1,y2),xlim=c(0,100),ylim=c(0,70), xlab="Explanatory variable",ylab="Response variable",type="n")
  35. points(x1,y1,col="red")
  36. points(x2,y2,col="blue",pch=16)
  37. abline(lm(y1~x1))
  38. abline(lm(y2~x2))
  39.  
  40. legend(locator(1),c("treatment","control"),pch=c(1,16), col=c("red","blue"))
  41. ##5.2.1##
  42. plot(0:10,0:10,xlim=c(0,32),ylim=c(0,40),type="n",xaxt="n",yaxt="n", xlab="",ylab="")
  43. x <- seq(1,31,2)
  44. s <- -16
  45. f <- -1 for (y in seq(2,40,2.5)) {
  46. s <- s+16
  47. f <- f+16 y2 <- rep(y,16) points(x,y2,pch=s:f,cex=0.7) text(x,y-1,as.character(s:f),cex=0.6) }
  48.  
  49. ##5.2.2##
  50. plot(0:9,0:9,pch=16,type="n",xaxt="n",yaxt="n",ylab="col",xlab="bg")
  51. axis(1,at=1:8)
  52. axis(2,at=1:8)
  53. for (i in 1:8) points(1:8,rep(i,8),pch=c(21,22,24),bg=1:8,col=i)
  54.  
  55. ##5.2.3##
  56. map.places <- read.csv("c:\\temp\\map.places.csv",header=T) #CHANGE FILE#
  57. attach(map.places)
  58. names(map.places)
  59.  
  60. map.data <- read.csv("c:\\temp\\bowens.csv",header=T) #CHANGE FILE#
  61. attach(map.data)
  62. names(map.data)
  63.  
  64. nn <- ifelse(north<60,north+100,north)
  65.  
  66. windows(9,7)
  67. plot(c(20,100),c(60,110),type="n",xlab="",ylab="",xaxt="n", yaxt="n")
  68. for (i in 1:length(wanted)){
  69. ii <- which(place == as.character(wanted[i]))
  70. text(east[ii], nn[ii], as.character(place[ii]), cex = 0.6) }
  71.  
  72. ##5.2.4##
  73. data <- read.table("c:\\temp\\sleep.txt",header=T) #CHANGE FILE#
  74. attach(data)
  75. plot(Days,Reaction)
  76.  
  77. s <- as.numeric(factor(Subject))
  78.  
  79. plot(Days,Reaction,type="n")
  80. for (k in 1:max(s)){
  81. x <- Days[s==k]
  82. y <- Reaction[s==k]
  83. lines(x,y,type="b",col="gray")
  84. }
  85.  
  86. ##5.2.5##
  87. sym <- rep(c(21,22,24),c(7,7,4))
  88. bcol <- c(2:8,2:8,2:5)
  89.  
  90. for (k in 1:max(s)){
  91. points(Days[s==k],Reaction[s==k],pch=sym[k],bg=bcol[k],col=1)
  92. }
  93.  
  94. data <- read.table("c:\\temp\\pgr.txt",header=T) #CHANGE FILE#
  95. attach(data)
  96. names(data)
  97.  
  98. plot(hay,pH)
  99. text(hay, pH, labels=round(FR, 2), pos=1, offset=0.5,cex=0.7)
  100.  
  101. plot(hay,pH,pch=16,col=ifelse(FR>median(FR),"red","black"))
  102. legend(locator(1),c("FR>median","FR<=median"),pch=16,col=c("red","black"))
  103.  
  104. ##5.2.6##
  105. smooth <- read.table("c:\\temp\\smoothing.txt",header=T) #CHANGE FILE#
  106. attach(smooth)
  107. names(smooth)
  108.  
  109. plot(x,y,pch=16)
  110. sequence <- order(x)
  111. lines(x[sequence],y[sequence])
  112.  
  113. plot(x,y,pch=16)
  114. lines(x,y)
  115.  
  116. ##5.2.7##
  117. x<-0:10
  118. y<-0:10
  119. plot(x,y)
  120.  
  121. lines(x,y,col="red")
  122. lines(x,y,col="blue",type="s")
  123. lines(x,y,col="green",type="S")
  124.  
  125.  
  126. ###5.3###
  127.  
  128. plot(0:10,0:10,xlab="",ylab="",xaxt="n",yaxt="n",type="n")
  129. rect(6,6,9,9)
  130.  
  131. ##5.3.1##
  132. corners <- function(){
  133. coos <- c(unlist(locator(1)),unlist(locator(1))) rect(coos[1],coos[2],coos[3],coos[4])
  134. }
  135. corners()
  136. arrows(1,1,3,8)
  137. arrows(1,9,5,9,code=3)
  138. arrows(4,1,4,6,code=3,angle=90)
  139.  
  140. click.arrows <- function(){
  141. coos <- c(unlist(locator(1)),unlist(locator(1))) arrows(coos[1],coos[2],coos[3],coos[4])
  142. }
  143. click.arrows()
  144. locations <- locator(6)
  145. class(locations)
  146. locations
  147.  
  148. polygon(locations,col="lavender")
  149.  
  150. ##5.3.2##
  151. z <- seq(-3,3,0.01)
  152. pd <- dnorm(z)
  153. plot(z,pd,type="l")
  154.  
  155. polygon(c(z[z<=-1],-1),c(pd[z<=-1],pd[z==-3]),col="red")
  156.  
  157. ###5.4###
  158. curve(3-3*x, -2, 2)
  159.  
  160. x <- seq(-2,2,0.01)
  161. y <-3-3*x
  162. plot(x,y,type="l")
  163.  
  164. ##5.4.1##
  165. xv <- 0:100
  166. yA <- 482*xv*exp(-0.045*xv)
  167. yB <- 518*xv*exp(-0.055*xv)
  168.  
  169. plot(c(xv,xv),c(yA,yB),xlab="stock",ylab="recruits",type="n")
  170. lines(xv,yA,lty=2,col="blue")
  171. lines(xv,yB,lty=1,col="red")
  172.  
  173. info <- read.table("c:\\temp\\plotfit.txt",header=T) #CHANGE FILE#
  174. attach(info)
  175. names(info)
  176.  
  177. points(x,y,pch=16)
  178.  
  179. ##5.4.2##
  180. data <- read.table("c:\\temp\\jaws.txt",header=T) #CHANGE FILE#
  181. attach(data)
  182. names(data)
  183.  
  184. par(mfrow=c(2,2))
  185.  
  186. plot(age,bone,pch=16,main="lowess")
  187. lines(lowess(age,bone),col="red")
  188.  
  189. plot(age,bone,pch=16,main="loess")
  190. model <- loess(bone~age)
  191. xv <- 0:50
  192. yv <- predict(model,data.frame(age=xv))
  193. lines(xv,yv,col="red")
  194.  
  195. library(mgcv)
  196. plot(age,bone,pch=16,main="gam")
  197. model <- gam(bone~s(age))
  198. xv <- 0:50
  199. yv <- predict(model,list(age=xv))
  200. lines(xv,yv,col="red")
  201.  
  202. plot(age,bone,pch=16,main="cubic polynomial"))
  203. model <- lm(bone~age+I(ageˆ2)+I(ageˆ3))
  204. xv <- 0:50
  205. yv <- predict(model,list(age=xv))
  206. lines(xv,yv,col="red")
  207.  
  208. ###5.5###
  209. data <- read.table("c:\\temp\\pollute.txt",header=T) #CHANGE FILE#
  210. attach(data)
  211.  
  212. par(mfrow=c(1,2))
  213. plot(Population,Pollution)
  214. plot(Temp,Pollution)
  215.  
  216. windows(7,4)
  217. par(mfrow=c(1,2))
  218. plot(Population,Pollution)
  219. plot(Temp,Pollution)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement