楼主: ReneeBK
2705 13

[休闲其它] 【独家发布】Spatial Data Analysis in Ecology and Agriculture using R [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4898份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49640 个
通用积分
55.8137
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2014-11-21 03:50:21 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

  • http://www.crcpress.com/product/isbn/9781439819135
  • http://www.plantsciences.ucdavis.edu/plant/sda.htm

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Agriculture Analysis culture Analysi Spatial

已有 1 人评分经验 收起 理由
苹果六人行 + 60 精彩帖子

总评分: 经验 + 60   查看全部评分

本帖被以下文库推荐

沙发
ReneeBK 发表于 2014-11-21 03:53:23
  1. # Creation of Fig. 1.4: Map of the yellow-billed cuckoo habitat
  2. library(maptools)
  3. library(rgdal)
  4. library(maps)
  5. data(stateMapEnv)
  6. cal.map <- map("state", "california",
  7.    fill=TRUE, col="transparent",  plot = FALSE)
  8. cal.poly <- map2SpatialPolygons(cal.map, "California")
  9. proj4string(cal.poly) <- CRS("+proj=longlat +datum=WGS84")
  10. data.Set1 <- readShapePoly("set1\\set1data.shp")
  11. proj4string(data.Set1) <- CRS("+proj=utm +zone=10 +ellps=WGS84")
  12. data.Set1.wgs <- spTransform(data.Set1, CRS("+proj=longlat
  13.   +datum=WGS84"))
  14. par(mai = c(1,1,1,1))
  15. plot(cal.poly, axes = FALSE) # Fig. 1.4
  16. title(main = "Data Set 1 Site Location",cex.main = 2)
  17. plot(data.Set1.wgs, add = TRUE)
  18. lines(c(-122.15,-121.9), c(39.7,39.7))
  19. lines(c(-122.15,-122.15), c(39.7,39.95))
  20. lines(c(-122.15,-121.9), c(39.95,39.95))
  21. lines(c(-121.9,-121.9), c(39.7,39.95))
  22. lines(c(-118,-113), c(36.3,36.3), lwd = 3)
  23. lines(c(-118,-113), c(41.9,41.9), lwd = 3)
  24. lines(c(-118,-118), c(36.3,41.9), lwd = 3)
  25. lines(c(-113,-113), c(36.3,41.9), lwd = 3)
  26. arrows(-118, 39.82, -121.85, 39.82, lwd = 3, length = 0.1)
  27. deg.per.km <- 360 / 40075
  28. lat <- 34
  29. text(-124, lat+0.35, "0")
  30. text(-122, lat+0.35, "200 km")
  31. deg.per.km <- deg.per.km * cos(pi*lat / 180)
  32. SpatialPolygonsRescale(layout.scale.bar(), offset = c(-124, lat),
  33.   scale = 200 * deg.per.km, fill=c("transparent","black"),
  34.   plot.grid = FALSE)
  35. SpatialPolygonsRescale(layout.north.arrow(), offset = c(-124, 36),
  36.   scale = 1, plot.grid = FALSE)

  37. #This is from Fig. 1.5 of Murrel (2006) to draw the small map
  38. par(fig = c(0.5, 0.94, 0.3, 0.95), new = TRUE)
  39. plot.new()
  40. xrange <- bbox(data.Set1.wgs)[1,]
  41. yrange <- bbox(data.Set1.wgs)[2,]
  42. plot.window(xlim = xrange, ylim = yrange)
  43. plot(data.Set1.wgs, add = TRUE, axes = FALSE)

  44. # --------------------------------------------

  45. # Creation of Fig. 1.5
  46. # Land classes in the site of Data Set 1

  47. library(maptools)
  48. library(rgdal)
  49. setwd("c:\\aardata\\book\\data")
  50. data.Set1.cover <- readShapePoly("set1\\landcover.shp")
  51. proj4string(data.Set1.cover) <- CRS("+proj=utm +zone=10 +ellps=WGS84")
  52. levels(data.Set1.cover$VegType)
  53. data.Set1.cover@data$VegType <-
  54.   factor(as.character(slot(data.Set1.cover, "data")$VegType),
  55.   labels = c("Grassland", "Cropland","Developed",
  56.   "Freshwater Wetland", "Gravel Bar", "Lacustrine", "Orchard",
  57.   "Riverine", "Riparian Forest", "Oak Woodland"))
  58. north <- list("SpatialPolygonsRescale", layout.north.arrow(),
  59.    offset = c(579800,4420000), scale = 600)
  60. scale <- list("SpatialPolygonsRescale", layout.scale.bar(),
  61.    offset = c(579400,4419000), scale = 1000,
  62.    fill = c("transparent", "black"))
  63. text1 <- list("sp.text", c(579400, 4419200), "0")
  64. text2 <- list("sp.text", c(580300, 4419200), "1 km")
  65. map.layout = list(north, text1, text2, scale)
  66. greys <- grey(c(180, 190, 140, 240, 150, 250, 130, 250, 30, 170) / 255)
  67. spplot(data.Set1.cover, "VegType", col.regions = greys,
  68.    main = "Data Set 1 Land Cover, Northern End, 1997",
  69.    xlim = c(576000,580500), ylim = c(4417400,4421000),
  70.    sp.layout = map.layout)

  71. # Rainbow color version
  72. spplot(data.Set1.cover, "VegType", col.regions = rainbow(10),
  73.    main = "Land Use, Northern End, 1997",
  74.    xlim = c(576000,580500), ylim = c(4417400,4421000),
  75.    sp.layout = map.layout)

  76. # Color version using RColorBrewer
  77. library(RColorBrewer)
  78. brown.bg <- brewer.pal(11,"BrBG")
  79. purple.green <- brewer.pal(11,"PiYG")
  80. color.array <- c(
  81.    purple.green[8], #Grassland
  82.    purple.green[4], #Cropland
  83.    purple.green[3], #Developed
  84.    brown.bg[6], #Freshwater Wetland
  85.    brown.bg[4], #Gravel Bar
  86.    brown.bg[8], #Lacustrine
  87.    purple.green[2], #Orchard
  88.    brown.bg[7], #Riverine
  89.    "yellow", #Riparian Forest
  90.    purple.green[11]) #Oak Woodland

  91. spplot(data.Set1.cover, "VegType", col.regions = color.array,
  92.    main = "Data Set 1 Land Cover, Northern End, 1997",
  93.    xlim = c(576000,580500), ylim = c(4417400,4421000),
  94.    sp.layout = map.layout)
复制代码


藤椅
ReneeBK 发表于 2014-11-21 03:54:58
  1. #Fig. 1.8
  2. library(maps)
  3. library(maptools)
  4. uy.map <- map("world", "uruguay",
  5.    fill=TRUE, col="transparent",  plot = FALSE)
  6. uy.poly <- map2SpatialPolygons(uy.map, "Uruguay")
  7. proj4string(uy.poly) <- CRS("+proj=longlat +datum=WGS84")
  8. data.Set3 <- read.csv("Set3\\Set3data.csv", header = TRUE)
  9. data.disp <- data.frame(Field = 1:16)
  10. data.disp$Longitude <- tapply(data.Set3$Longitude, data.Set3$Field, mean)
  11. data.disp$Latitude <- tapply(data.Set3$Latitude, data.Set3$Field, mean)
  12. data.dispN <- data.disp[which(data.disp$Latitude > -32.8316),]
  13. data.dispS <- data.disp[which(data.disp$Latitude < -33.7008),]
  14. data.dispC <- data.disp[which(data.disp$Latitude < -32.8314 &
  15.   data.disp$Latitude > -33.7007),]
  16. data.dispN$Longitude <- data.dispN$Longitude + 4 +
  17.   30  * (data.dispN$Longitude + 53.7548)
  18. data.dispN$Latitude <- data.dispN$Latitude + 3 +
  19.   30  * (data.dispN$Latitude + 32.8316)
  20. data.dispC$Longitude <- data.dispC$Longitude + 7 +
  21.   45  * (data.dispC$Longitude + 53.90967)
  22. data.dispC$Latitude <- data.dispC$Latitude +
  23.   45  * (data.dispC$Latitude + 33.21518)
  24. data.dispS$Longitude <- data.dispS$Longitude + 3 +
  25.   30  * (data.dispS$Longitude + 54.51)
  26. data.dispS$Latitude <- data.dispS$Latitude - 2 +
  27.   15  * (data.dispS$Latitude + 33.72)
  28. coordinates(data.disp) <- c("Longitude", "Latitude")
  29. proj4string(data.disp) <- CRS("+proj=longlat +datum=WGS84")
  30. coordinates(data.dispN) <- c("Longitude", "Latitude")
  31. proj4string(data.dispN) <- CRS("+proj=longlat +datum=WGS84")
  32. coordinates(data.dispC) <- c("Longitude", "Latitude")
  33. proj4string(data.dispC) <- CRS("+proj=longlat +datum=WGS84")
  34. coordinates(data.dispS) <- c("Longitude", "Latitude")
  35. proj4string(data.dispS) <- CRS("+proj=longlat +datum=WGS84")
  36. plot(uy.poly, xlim = c(-59, -46), axes = FALSE)
  37. title(main = "Data Set 3 Field Locations", cex.main = 2)
  38. plot(data.disp, add = TRUE, pch = 1, cex = 0.5)
  39. plot(data.dispN, add = TRUE, pch = 1)
  40. plot(data.dispC, add = TRUE, pch = 1)
  41. plot(data.dispS, add = TRUE, pch = 1)
  42. text(coordinates(data.dispN)[,1]+0.25, coordinates(data.dispN)[,2]+0.25,
  43.    as.character(data.dispN$Field))
  44. text(coordinates(data.dispC)[,1]+0.25, coordinates(data.dispC)[,2]+0.25,
  45.    as.character(data.dispC$Field))
  46. text(coordinates(data.dispS)[,1]+0.25, coordinates(data.dispS)[,2]+0.25,
  47.    as.character(data.dispS$Field))
  48. lines(c(-51.8,-49), c(-30,-30))
  49. lines(c(-51.8,-49), c(-28,-28))
  50. lines(c(-51.8,-51.8), c(-30,-28))
  51. lines(c(-49,-49), c(-30,-28))
  52. arrows(-51.8,-29.7, -53.65,-32.65, length = 0.1)
  53. lines(c(-51.1,-46.3), c(-34.2,-34.2))
  54. lines(c(-51.1,-46.3), c(-31.9,-31.9))
  55. lines(c(-51.1,-51.1), c(-34.2,-31.9))
  56. lines(c(-46.3,-46.3), c(-31.9,-34.2))
  57. arrows(-51.1,-33, -53.8,-33.2, length = 0.1)
  58. lines(c(-52.1,-50.5), c(-36.45,-36.45))
  59. lines(c(-52.1,-50.5), c(-35,-35))
  60. lines(c(-52.1,-52.1), c(-36.45,-35))
  61. lines(c(-50.5,-50.5), c(-36.45,-35))
  62. arrows(-52.1,-35.5, -54.4,-33.8, length = 0.1)

  63. deg.per.km <- 360 / 40075
  64. lat <- -36
  65. text(-57, lat+0.35, "0")
  66. text(-55, lat+0.35, "200 km")
  67. deg.per.km <- deg.per.km * cos(pi*lat / 180)
  68. SpatialPolygonsRescale(layout.scale.bar(), offset = c(-57, lat),
  69.   scale = 200 * deg.per.km, fill=c("transparent","black"),
  70.   plot.grid = FALSE)
  71. SpatialPolygonsRescale(layout.north.arrow(), offset = c(-55, -30),
  72.   scale = 1, plot.grid = FALSE)
复制代码


板凳
ReneeBK 发表于 2014-11-21 03:55:35
  1. # Figure 1.1
  2. # Display of 1/10 of points in Data Set 2
  3. library(rgdal)
  4. library(maptools)
  5. # Hillshade map downloaded from UCSB Biogeograph Lab
  6. # http://www.biogeog.ucsb.edu/projects/gap/gap_data_state.html
  7. # and gepregistered in ArcGIS
  8. ca.elev <- readGDAL("Auxiliary\\caelev.tif")
  9. proj4string(ca.elev) <- CRS("+proj=longlat +datum=WGS84")
  10. greys <- grey(0:255 / 255)
  11. par(mai = c(1,1,1,1))
  12. image(ca.elev, col = greys, axes = TRUE) # Fig. 1.1
  13. # From Appendix B/Set2 Input
  14. data.Set2 <- read.csv("set2\\set2data.csv", header = TRUE)
  15. # Assign ID values and use the modulo function to select 1 in 10
  16. data.Set2$ID <- 1:nrow(data.Set2)
  17. Set2.plot <- data.Set2[-which(data.Set2$ID %% 10 != 0),]
  18. coordinates(Set2.plot) <- c("Longitude", "Latitude")
  19. proj4string(Set2.plot) <- CRS("+proj=longlat +datum=WGS84")
  20. plot(Set2.plot, pch = 16, cex = 0.5, add = TRUE, col = "white") # Fig. 1.1
  21. title(main = "Wieslander Survey Locations", cex.main = 2, xlab = "Longitude",
  22.    ylab = "Latitude", cex.lab = 1.5)

  23. # Color version of Fig.1.1
  24. library(RColorBrewer)
  25. col.terrain <- terrain.colors(10)[10:1]
  26. col.terrain[10] <- "white"
  27. image(ca.elev, col = col.terrain, axes = TRUE)
  28. plot(Set2.plot, pch = 16, cex = 0.5, add = TRUE, col = "red")
  29. title(main = "Wieslander Survey Locations", cex.main = 2, xlab = "Longitude",
  30.    ylab = "Latitude", cex.lab = 1.5)
  31.    
  32. # -----------------------------------------------------------

  33. # Kriging of Field 4-2 ECa to create Fig. 1.2a
  34. library(gstat)
  35. library(maptools)
  36. data.Set4.2EC <- read.csv("Set4\\Set4.2EC.csv", header = TRUE)
  37. data.Set4.2EC$ID <- 1:nrow(data.Set4.2EC)
  38. # Establish the coordinates of the rectangular boundary
  39. N <- 4267868
  40. S <- 4267513
  41. E <- 592867
  42. W <- 592082
  43. cell.size <- 5
  44. # Create the interpolation grid
  45. grid.xy <- expand.grid(x = seq(W, E, cell.size),  y = seq(N, S, -cell.size))
  46. coordinates(grid.xy) <- ~x + y
  47. gridded(grid.xy) = TRUE
  48. # Select every fifth data value
  49. data.vgm <- data.Set4.2EC[-which(data.Set4.2EC$ID %% 5 != 0),]
  50. coordinates(data.vgm) <- c("Easting", "Northing")
  51. EC.vgm <- variogram(ECto30 ~ 1, data.vgm)
  52. EC.fit <- fit.variogram(EC.vgm, model = vgm(100000, "Sph", 700,10000))
  53. plot(EC.vgm, EC.fit, col = "black") # Fig.1.2a
  54. EC.krig <- krige(ECto30 ~ 1, data.vgm, grid.xy, model = EC.fit)
  55. greys <- grey(0:255 / 255)
  56. spplot(EC.krig["var1.pred"], col.regions = greys, # Fig. 1.2a
  57.   xlim = c(W,E), ylim = c(S,N), scales = list(draw = TRUE),
  58.   xlab = "Easting", ylab = "Northing",
  59.   main = "Soil Apparent Electrical Conductivity (mS/m)")
  60.   
  61. # --------------------------------------------------

  62. # Plot of Field 4-2 NDVI to create Fig. 1.2b
  63. library(gstat)
  64. library(rgdal)
  65. data.4.2.May <- readGDAL("set4\\Set4.20596.tif")
  66. N <- 4267868
  67. S <- 4267513
  68. E <- 592867
  69. W <- 592082
  70. # Create a data frame of image values
  71. img.df <- data.frame(data.4.2.May@data$band1)
  72. # Determine the inage coordinates
  73. img.coords <- coordinates(data.4.2.May)
  74. # Assign these coordinantes to the data frame
  75. img.df$x <- img.coords[,1]
  76. img.df$y <- img.coords[,2]
  77. # Convert to a SpatialPointsDataFrame
  78. coordinates(img.df) <- c("x", "y")
  79. # Create a boundary polygon
  80. coords.mat <- matrix(c(W,E,E,W,W,N,N,S,S,N), ncol = 2)
  81. # Create a list to hold the corner coords
  82. bdry.vec <- vector(mode="list", length=1)
  83. # Build the objects leading to a SpatialPolygonsDataFrame
  84. bdry.vec[[1]] <- Polygons(list(Polygon(coords.mat)), ID="1")
  85. bdry.poly <- SpatialPolygons(bdry.vec,
  86.    proj4string = CRS("+proj=utm +zone=10 +ellps=WGS84"))
  87. # Exclude points outside the boundary
  88. in.f <- overlay(img.df,bdry.poly)
  89. f.img <- img.df[which(!is.na(in.f)),]
  90. names(f.img@data) <- "IR"
  91. gridded(f.img) <- TRUE
  92. greys <- grey(0:255 / 255)
  93. spplot(f.img, zcol = "IR", col.regions = greys, # Fig. 1.2b
  94.   xlim = c(W,E),  ylim = c(S,N), scales = list(draw = TRUE),
  95.   xlab = "Easting", ylab = "Northing", main = "Infrared Band Digital Number")
  96.   
  97. # Construction of Fig. 1.2c
  98. # Interpolate yield to a 5 meter grid
  99. data.Yield4.2 <- read.csv("created\\Set4.2yld96cleaned.csv")
  100. data.Yield4.2$ID <- 1:nrow(data.Set4.2.Yield)
  101. cell.size <- 5
  102. grid.xy <- expand.grid(x = seq(W, E, cell.size), y = seq(N, S, -cell.size))
  103. coordinates(grid.xy) <- ~x + y
  104. gridded(grid.xy) = TRUE
  105. proj4string(grid.xy) <- CRS("+proj=utm +zone=10 +ellps=WGS84")
  106. # Remove yield trend prior to kriging
  107. data.Yield4.2$x <- with(data.Yield4.2, Easting - min(Easting))
  108. data.Yield4.2$y <- with(data.Yield4.2, Northing - min(Northing))
  109. trend.lm <- lm(Yield ~ x + y + I(x^2) + I(y^2) +
  110.    I(x*y), data = data.Yield4.2)
  111. data.Yield4.2$trend <- fitted(trend.lm)
  112. data.Yield4.2$YieldDT <- data.Yield4.2$Yield - fitted(trend.lm)
  113. # Select every 10th value
  114. krig.dat <- data.Yield4.2[-which(data.Yield4.2$ID %% 20 != 0),]
  115. coordinates(krig.dat) <- c("Easting", "Northing")
  116. proj4string(krig.dat) <- CRS("+proj=utm +zone=10 +ellps=WGS84")
  117. # Compute kriged detrended yield
  118. yield.vgm <- variogram(YieldDT ~ 1, krig.dat)
  119. yield.fit <- fit.variogram(yield.vgm, model = vgm(100000, "Sph", 200,10000))
  120. # Check the fit visually and compute kriged interploation
  121. plot(yield.vgm, yield.fit, col = "black")
  122. yield.krig <- krige(YieldDT ~ 1, krig.dat, grid.xy, model = yield.fit)
  123. # Add trend back
  124. x <- coordinates(yield.krig)[,1] - W
  125. y <- coordinates(yield.krig)[,2] - S
  126. b <- coef(trend.lm)
  127. yield.trend <- b[1] + b[2]*x + b[3]*y + b[4]*x^2 + b[5]*y^2 + b[6]*x*y
  128. yield.krig@data$var1.pred <-
  129.    yield.krig@data$var1.pred + yield.trend
  130. greys <- grey(255:0 / 255)
  131. spplot(yield.krig["var1.pred"], col.regions = greys,
  132.   xlim = c(W,E), ylim = c(S,N), scales = list(draw = TRUE),
  133.   xlab = "Easting", ylab = "Northing",
  134.   main = "Grain Yield (kg/ha)")  # Fig. 1.2c





  135.   
复制代码


报纸
ReneeBK 发表于 2014-11-21 03:56:27
  1. # Fig. 1.3a: Kriged clay content in Field 4.2
  2. library(gstat)
  3. library(maptools)
  4. data.Set4.2 <- read.csv("set4\\set4.296sample.csv", header = TRUE)
  5. # Establish the coordinates of the rectangular boundary
  6. N <- 4267868
  7. S <- 4267513
  8. E <- 592867
  9. W <- 592082
  10. cell.size <- 5
  11. # Create the interpolation grid
  12. grid.xy <- expand.grid(x = seq(W, E, cell.size),  y = seq(N, S, -cell.size))
  13. coordinates(grid.xy) <- ~x + y
  14. gridded(grid.xy) = TRUE
  15. coordinates(data.Set4.2) <- c("Easting", "Northing")
  16. Clay.vgm <- variogram(Clay ~ 1, data.Set4.2)
  17. Clay.fit <- fit.variogram(Clay.vgm, model = vgm(100000, "Sph", 700,10000))
  18. plot(Clay.vgm, Clay.fit, col = "black")
  19. Clay.krig <- krige(Clay ~ 1, data.Set4.2, grid.xy, model = Clay.fit)
  20. pts.list = list("sp.points", data.Set4.2, pch = 19, col = "black",
  21.    cex = 0.5)
  22. greys <- grey(255:50 / 255)
  23. spplot(Clay.krig["var1.pred"], col.regions = greys, # Fig. 1.3a
  24.   xlim = c(W,E), ylim = c(S,N), scales = list(draw = TRUE),
  25.   xlab = "Easting", ylab = "Northing",  sp.layout = list(pts.list),
  26.   main = "Field 4.2 Clay Content (Percent)")

  27. # Fig. 1.3b: Thiessen polygons for sample points in Field 4.2
  28. library(spatstat)
  29. # Create the Thiessen polygons
  30. cell.ppp <- ppp(coordinates(data.Set4.2)[,1], coordinates(data.Set4.2)[,2],
  31.      window = owin(c(W, E), c(S, N)))
  32. thsn.pp <- dirichlet(cell.ppp)
  33. thsn.sp <- as(thsn.pp, "SpatialPolygons")
  34. row.names(thsn.sp) <- row.names(data.Set4.2@data)
  35. # Transfer the data to create a SP Data Frame
  36. thsn.shp <- SpatialPolygonsDataFrame(thsn.sp, data.Set4.2@data)
  37. pts.list = list("sp.points", data.Set4.2, pch = 19, col = "black",
  38.    cex = 0.5)
  39. greys <- grey(255:50 / 255)
  40. spplot(thsn.shp, "Clay", col.regions = greys,  scales = list(draw = TRUE),
  41.   xlab = "Easting", ylab = "Northing", sp.layout = list(pts.list),
  42.   main = "Field 4.2 Clay Content (Percent)")
复制代码


地板
ReneeBK 发表于 2014-11-21 04:00:42
  1. # Assign the value of 3 to w
  2. w <- 3
  3. w # Display the value of w

  4. class(w) # Display the object class of w

  5. # Assign z a character value
  6. z <- "a"
  7. z
  8. class(z)

  9. z <- a

  10. class(class)

  11. # Example of the concatenate function
  12. z <- c(1, 7, 6.2, 4.5, -27, 1.5e2, 7251360203, w, 2*w, w^2, -27/w)
  13. z

  14. z[6]
  15. z[c(6,8)]

  16. # Remove the 6th element of z
  17. z[-6]
  18. z[-(6:11)]

  19. # This causes an error
  20. v[1] <- 4
  21. # This works
  22. v <- numeric()
  23. v[1] <- 4
  24. v[2] <- 6.2
  25. v

  26. # Assign v to z and display the result
  27. print(z <- v)

  28. print(w <- 1:30)

  29. print(z <- 30:1)

  30. print(a <- matrix(data = 1:9, nrow = 3))
  31. a[1,]
  32. 2 * a[1,]

  33. w <- c(1,3,6)
  34. z <- 1:3
  35. cbind(w, z)
复制代码


7
ReneeBK 发表于 2014-11-21 04:03:12
  1. data.Set1.obs <- read.csv("set1\\obspts.csv", header = TRUE)
  2. class(data.Set1.obs)
  3. str(data.Set1.obs)


  4. library(sp)
  5. coordinates(data.Set1.obs) <- c("Easting", "Northing")
  6. class(data.Set1.obs)
  7. str(data.Set1.obs)

  8. coordinates(data.Set1.obs)
  9. class(slot(data.Set1.obs, "data"))
  10. slot(data.Set1.obs, "data")$Abund[1:10]

  11. data.Set1.obs@data$Abund[1:10]

  12. ############################

  13. #Field 4.2
  14. N <- 4267873
  15. S <- 4267483
  16. E <- 592860
  17. W <- 592080

  18. print(coords.mat <- matrix(c(W,E,E,W,W,N,N,S,S,N),
  19.   ncol = 2))

  20. library(maptools)
  21. bdry.vec <- vector(mode="list", length=1)
  22. bdry.vec[[1]] <- Polygons(list(Polygon(coords.mat)), ID="1")
  23. bdry.sp <- SpatialPolygons(bdry.vec,
  24.    proj4string = CRS("+proj=utm +zone=10 +ellps=WGS84"))
  25. bdry.spdf <- SpatialPolygonsDataFrame(bdry.sp,
  26.    data = data.frame(ID ="1", row.names = "1"))
  27. # Putting a "." in the file name doesn't work
  28. # You don't have to include the ".shp", but you can
  29. writePolyShape(bdry.spdf,"created\\Set42bdry")

  30. library(rgdal)
  31. bdry.wgs <- spTransform(bdry.spdf, CRS("+proj=longlat
  32.   +datum=WGS84"))

  33. str(bdry.sp)
  34. str(bdry.spdf)
复制代码


8
ReneeBK 发表于 2014-11-21 04:05:35
  1. #Simulation of the 3 component model (deterministic+correlated+random)

  2. #Correlated data
  3. set.seed(123)
  4. sigma.eta <- 1.0  # Variance of autocorrelated component
  5. lambda <- 0.4     # Autocorrelation coefficient
  6. library(spdep)
  7. nlist <- cell2nb(20, 20)
  8. e <- rnorm(20^2)
  9. eta <- sigma.eta * invIrM(nlist, lambda) %*% e

  10. #Random data
  11. sigma.eps <- 0.4 # Magnitude of random component
  12. eps <- sigma.eps * rnorm(20^2)

  13. # Create the correlated component plus uncorrelated component
  14. Y.df <- data.frame(eta)  # Start with autocorr. comp.
  15. Y.df$eps <- eps  # Add uncorrelated component

  16. #Add deterministic logistic trend
  17. a <- 2
  18. b.eps <- 0.5  # Variance factor for uncorr. component
  19. b.eta <- 0.15 # Variance factor for autocorr. component
  20. c <- 1
  21. f2 <- 20 / 2
  22. xymax <- 20 - 0.5
  23. coords.xy <- expand.grid(x = 0.5:xymax, y = xymax:0.5)
  24. x <- coords.xy[,1]
  25. y <- coords.xy[,2]
  26. Y.trend <- a * exp(c * (x - f2)) / (1 + exp(c * (x - f2)))
  27. Y <- Y.trend + b.eps * eps + b.eta* eta
  28. Y.df$trend <- Y.trend
  29. Y.df$Y <- Y
  30. Y.df$x <- x
  31. Y.df$y <- y

  32. # Create Fig. 3.1
  33. Y.plot <- Y.df[(Y.df$y == 20 / 2 + 0.5),]
  34. Yt <- Y.plot$trend
  35. Yeps <- b.eps * Y.plot$eps
  36. Yeta <- b.eta * Y.plot$eta
  37. Y <- Yt + Yeps + Yeta
  38. x <- Y.plot$x
  39. par(mai = c(1,1,1,1))
  40. plot(x, Y, type = "l", lwd = 1, cex.lab = 1.5,  #Fig. 3.1
  41.    ylim = c(-0.5,2.5), xlab = expression(italic(x)),
  42.    ylab = expression(italic(Y)))
  43. lines(x, Yt, lwd = 3)
  44. lines(x, Yt + Yeta, lwd = 2)
  45. text(x = 12.5, y = 0.25, "T(x,y)", pos = 4)
  46. lines(c(10,12.5),c(0.25,0.25),lwd = 3)
  47. text(x = 12.5, y = 0.0, expression("T(x,y)"~+~eta*"(x,y)"),
  48.    pos = 4)
  49. lines(c(10,12.5),c(0,0),lwd = 2)
  50. text(x = 12.5, y = -0.25, pos = 4,
  51.    expression("T(x,y)"~+~eta*"(x,y)"~+~epsilon*"(x,y)"),)
  52. lines(c(10,12.5),c(-0.25,-0.25),lwd = 1)

  53. # --------------------------------------

  54. # Perspective plots (Fig. 3.2)
  55. # Plot using the function persp

  56. x <- (1:20) + 1.5
  57. y <- x

  58. # Original data (Fig. 3.2a)
  59. Y.persp <- matrix(4*Y.df$Y, nrow = 20)
  60. persp(x, y, Y.persp, theta = 225, phi = 15,
  61.   scale = FALSE, zlab = "Y")  #Fig. 3.2a

  62. # Trend T(x,y) (Fig. 3.2b)
  63. Trend <- matrix(4*Y.df$trend, nrow = 20)
  64. persp(x, y, Trend, theta = 225, phi = 15, scale = FALSE,
  65.    zlab = "T(x,y)")  #Fig. 3.2b

  66. # Fit linear trend to logistic model (Fig. 3.2c)
  67. # Create the data with threecomponent.r
  68. model.lin <- lm(Y ~ x + y, data = Y.df)
  69. trend.lin <- predict(model.lin)
  70. Y.lin <- matrix(trend.lin, nrow = 20)
  71. Fit <- 4 * Y.lin
  72. persp(x, y, Fit, theta = 225, phi = 15, scale = FALSE)

  73. # Median polish trend  (Fig. 3.2d)
  74. Y.trend <- matrix(Y.df$Y, nrow = 20)
  75. model.mp <- medpolish(Y.trend)
  76. Y.mp <- Y.trend - model.mp$residuals
  77. Fit <- 4 * Y.mp
  78. persp(x, y, Fit, theta = 225, phi = 15, scale = FALSE)

  79. # ---------------------------------------------

  80. #Trend plus random component for Field 4.1

  81. # Read data using code in Appendix B.4
  82. data.Set4.1 <- read.csv("Set4\\set4.196sample.csv", header = TRUE)
  83. Sand <- matrix(nrow = 13, ncol = 7)
  84. for (i in 1:86){
  85.   Sand[data.Set4.1$Row[i],data.Set4.1$Column[i]] <-
  86.     data.Set4.1$Sand[i]
  87. }
  88. North <- 3 * 1:13
  89. West <- 3 * 1:7
  90. persp(North, West, Sand, theta = 30, phi = 20,
  91.   zlim = c(0,45), scale = FALSE)  # Fig. 3.3a
  92.   
  93. # Linear model (quadratic)
  94. trend.lm <- lm(Sand ~ Row + Column + I(Row^2) +
  95.   I(Column^2) + I(Row*Column), data = data.Set4.1)
  96. b <- coef(trend.lm)
  97. Trend <- matrix(nrow = 13, ncol = 7)
  98. for (i in 1:8){
  99.    for (j in 1:7){
  100.       Trend[i,j] <- b[1] + b[2]*i + b[3]*j + b[4]*i^2 + b[5]*j^2 + b[6]*i*j
  101. }}
  102. for (i in 9:13){
  103.    for (j in 1:6){
  104.       Trend[i,j] <- b[1] + b[2]*i + b[3]*j + b[4]*i^2 + b[5]*j^2 + b[6]*i*j
  105. }}
  106. persp(North, West, Trend, theta = 30, phi = 20,
  107.   zlim = c(0,45), scale = FALSE) # Fig. 3.3b


  108. #Median polish for Field 4.1
  109. #Break into 2 parts, one with 7 columns and one with 6
  110. Sand1 <- Sand[1:8,]
  111. Sand2 <- Sand[9:13,1:6]
  112. trend1 <- medpolish(Sand1)
  113. trend2 <- medpolish(Sand2)
  114. T1.mp <- matrix(nrow = 8, ncol = 7)
  115. for (i in 1:8){
  116.    for (j in 1:7){
  117.       T1.mp[i,j] <- trend1$row[i] + trend1$col[j] + trend1$overall
  118. }}
  119. T2.mp <- matrix(nrow = 5, ncol = 7)
  120. for (i in 1:5){
  121.    for (j in 1:6){
  122.       T2.mp[i,j] <- trend2$row[i] + trend2$col[j] + trend2$overall
  123. }}
  124. MedPolish <- rbind(T1.mp,T2.mp)
  125. persp(North, West, MedPolish, theta = 30, phi = 20, zlim = c(0,45),
  126.   scale = FALSE)   # Fig. 3.3c
  127.   
  128. # --------------------------------------------

  129. # Bubble plots of Field 4.1 sand content
  130. #Fig. 3.4a
  131. library(maptools)
  132. # The shapefile set419697bdry.shp is created in Exercise 2.11
  133. bdry.spdf <- readShapePoly("created\\set419697bdry.shp")
  134. coordinates(data.Set4.1) <- c("Easting", "Northing")
  135. par(mai = c(1,1,1,1))
  136. plot(bdry.spdf, axes = TRUE) #Fig. 3.4a
  137. plot(data.Set4.1, add = TRUE, pch = 1,
  138.   cex =  (data.Set4.1$Sand / 15))
  139. title(main = "Field 4.1 Sand Content", cex.main = 2, xlab = "Easting",
  140.    ylab = "Northing", cex.lab = 1.5)
  141. legend(592450, 4270600, c("15", "30", "45"), pt.cex = 1:3, pch = 1,
  142.   y.intersp = 1.5, title = "Percent Sand")

  143. i <- data.Set4.1$Row
  144. j <- data.Set4.1$Column
  145. data.Set4.1$lin <- data.Set4.1$Sand - (b[1] + b[2]*i + b[3]*j +
  146.   b[4]*i^2 + b[5]*j^2 + b[6]*i*j)
  147. plot(bdry.spdf, axes = TRUE) #Fig. 3.4b
  148. plot(data.Set4.1, add = TRUE, pch = 1,
  149.   cex = (1 + data.Set4.1$lin / 10))
  150. title(main = "Linear Detrending", cex.main = 2, xlab = "Easting",
  151.    ylab = "Northing", cex.lab = 1.5)
  152. legend(592450, 4270600, c("-10","0", "10"), pt.cex = c(0.1,1,2), pch = 1,
  153.   y.intersp = 1, title = "Percent Sand")
  154.   
  155. # Concatenate the columns of T1.mp into a vector
  156. stack(data.frame(T1.mp))[,1] # Chack that the concat. is correct
  157. data.Set4.1$mp <- data.Set4.1$Sand -
  158.    c(stack(data.frame(T1.mp))[,1],stack(data.frame(T2.mp))[1:30,1])
  159. plot(bdry.spdf, axes = TRUE) #Fig. 3.4c
  160. plot(data.Set4.1, add = TRUE, pch = 1,
  161.   cex = (1 + data.Set4.1$mp / 10))
  162. title(main = "Median Polish Detrending", cex.main = 2, xlab = "Easting",
  163.    ylab = "Northing", cex.lab = 1.5)
  164. legend(592450, 4270600, c("-10","0", "10"), pt.cex = c(0.1,1,2), pch = 1,
  165.   y.intersp = 1, title = "Percent Sand")
复制代码


9
lonestone 在职认证  发表于 2014-11-21 06:04:31 来自手机
ReneeBK 发表于 2014-11-21 03:50
  • http://www.crcpress.com/product/isbn/9781439819135
  • http://www.plantsciences.ucdavis.edu/p ...
  • лл

    10
    fjrong 在职认证  发表于 2014-11-21 08:42:58 来自手机
    ReneeBK 发表于 2014-11-21 03:50
  • http://www.crcpress.com/product/isbn/9781439819135
  • http://www.plantsciences.ucdavis.edu/p ...
  • 谢谢分享

    您需要登录后才可以回帖 登录 | 我要注册

    本版微信群
    jg-xs1
    拉您进交流群
    GMT+8, 2026-1-6 12:39