Spectral Analysis

Author

Andres Calderon

Spectral analysis

Leemos los datos…

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
✓ ggplot2 3.3.5     ✓ purrr   0.3.4
✓ tibble  3.1.6     ✓ dplyr   1.0.8
✓ tidyr   1.2.0     ✓ stringr 1.4.0
✓ readr   2.1.2     ✓ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
pixels <- read_csv("~/Downloads/new_data_filtered.csv")
Rows: 154544 Columns: 12
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (1): clase
dbl (11): B2, B3, B4, B5, B6, B7, B8, B11, B12, DEM, Slopes

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Registramos algunas funciones para obtener la desviacion estandar por debajo y por encima del promedio y para extraer los registros para cada clase…

##
getSDRange <- function(data, the_class, sd_factor){
  sample <- data |> 
    filter(clase == the_class) |> 
    mutate(BDEM = DEM, BSlope = Slopes) |>
    select(B2, B3, B4, B5, B6, B7, B8, B11, B12, BDEM, BSlope)
  print(summary(sample))
  print(sample |> tally())
  
  sds   = sapply(sample, sd)
  means = sapply(sample, mean)
  stats = bind_rows(means + (sd_factor * sds), means, means - (sd_factor * sds)) |> 
    mutate(id = row_number()) |> 
    pivot_longer(cols = starts_with("B"), 
                 names_to = "band", values_to = "value") |>
    mutate(band = factor(
      band, 
      levels = c("B2","B3","B4","B5","B6","B7","B8","B9","B11","B12", "BDEM", "BSlope"))
    )
  
  return(stats)
}

##
plotClass <- function(the_class){
  bands <- pixels |> 
    filter(clase == the_class) |> 
    mutate(BDEM = DEM, BSlope = Slopes) |>
    select(B2, B3, B4, B5, B6, B7, B8, B11, B12, BDEM, BSlope) |>
    mutate(id = row_number()) |>
    pivot_longer(cols = starts_with("B"), names_to = "band", values_to = "value") |>
    mutate(clase = the_class, 
           band = factor(
             band, 
             levels = c("B2","B3","B4","B5","B6","B7","B8","B9","B11","B12", "BDEM", "BSlope")
           )
    )
  
  return(bands)
}

Graficamos los cinco numeros para las clases principales…

palma <- plotClass("palma")
cafe1 <- plotClass("cafe_expuesto")
cafe2 <- plotClass("cafe_semisombra")
cafe3 <- plotClass("cafe_sombra")
ggplot(data = palma, aes(x=band, y= value, fill=clase)) + geom_boxplot()

ggplot(data = cafe1, aes(x=band, y= value, fill=clase)) + geom_boxplot()

ggplot(data = cafe2, aes(x=band, y= value, fill=clase)) + geom_boxplot()

ggplot(data = cafe3, aes(x=band, y= value, fill=clase)) + geom_boxplot()

y para las demas…

vege <- plotClass("vegetacion")
ciud <- plotClass("ciudad")
agua <- plotClass("agua")
ggplot(data = vege, aes(x=band, y= value, fill=clase)) + geom_boxplot()

ggplot(data = agua, aes(x=band, y= value, fill=clase)) + geom_boxplot()

ggplot(data = ciud, aes(x=band, y= value, fill=clase)) + geom_boxplot()

Vemos como se distribuye los 3 tipos de cafe…

##
cafe <- cafe1 |> bind_rows(cafe2) |> bind_rows(cafe3)
ggplot(data = cafe, aes(x=band, y= value, fill=clase)) + geom_boxplot()

Registramos una funcion para graficar las firmas espectrales para las clases con su promedio y +1.5 y -1.5 desviaciones estandar…

##
runGgplot <- function(data, the_class, factor){
  range = getSDRange(pixels, the_class, factor)
  g = ggplot(data = data, aes(x=band, y= value, group=id)) + 
    geom_line(size=0.2, alpha=0.01) +
    stat_summary(aes(y = value, group = 1), 
                 fun = mean, colour = "blue", geom = "line", group = 1) +
    geom_line(data = range |> filter(id == 1), 
              aes(x = band, y = value), colour= "red", linetype = "dashed") +
    geom_line(data = range |> filter(id == 3), 
              aes(x = band, y = value), colour= "red", linetype = "dashed") +
    theme_bw() 
  
  return(g)
}

Graficamos las firmas espectrales para las clases principales…

factor_default = 1.5

plot(runGgplot(palma, "palma", factor_default))
       B2              B3              B4              B5        
 Min.   : 82.0   Min.   :277.0   Min.   :108.0   Min.   : 507.0  
 1st Qu.:163.0   1st Qu.:367.0   1st Qu.:210.0   1st Qu.: 656.0  
 Median :190.0   Median :401.0   Median :243.0   Median : 710.0  
 Mean   :194.9   Mean   :412.7   Mean   :279.3   Mean   : 736.2  
 3rd Qu.:218.0   3rd Qu.:445.0   3rd Qu.:301.0   3rd Qu.: 797.0  
 Max.   :377.0   Max.   :628.0   Max.   :785.0   Max.   :1089.0  
       B6             B7             B8            B11            B12        
 Min.   :1969   Min.   :2164   Min.   :2212   Min.   :1219   Min.   : 475.0  
 1st Qu.:2338   1st Qu.:2930   1st Qu.:3006   1st Qu.:1553   1st Qu.: 650.0  
 Median :2493   Median :3180   Median :3276   Median :1656   Median : 720.0  
 Mean   :2488   Mean   :3146   Mean   :3238   Mean   :1719   Mean   : 786.6  
 3rd Qu.:2643   3rd Qu.:3402   3rd Qu.:3510   3rd Qu.:1817   3rd Qu.: 860.0  
 Max.   :2931   Max.   :3826   Max.   :3964   Max.   :2634   Max.   :1643.0  
      BDEM           BSlope      
 Min.   : 52.0   Min.   :0.5239  
 1st Qu.: 80.0   1st Qu.:2.4313  
 Median : 93.0   Median :3.4991  
 Mean   : 92.7   Mean   :3.7145  
 3rd Qu.:103.0   3rd Qu.:4.7911  
 Max.   :135.0   Max.   :7.8437  
# A tibble: 1 × 1
      n
  <int>
1  9951

plot(runGgplot(cafe1, "cafe_expuesto", factor_default))
       B2              B3              B4            B5               B6      
 Min.   : 37.0   Min.   :167.0   Min.   : 46   Min.   : 443.0   Min.   :1398  
 1st Qu.:154.0   1st Qu.:342.0   1st Qu.:196   1st Qu.: 695.0   1st Qu.:2079  
 Median :198.0   Median :419.0   Median :255   Median : 818.0   Median :2422  
 Mean   :207.2   Mean   :429.1   Mean   :279   Mean   : 837.7   Mean   :2481  
 3rd Qu.:251.0   3rd Qu.:507.0   3rd Qu.:342   3rd Qu.: 966.0   3rd Qu.:2826  
 Max.   :434.0   Max.   :775.0   Max.   :625   Max.   :1362.0   Max.   :3844  
       B7             B8            B11            B12              BDEM     
 Min.   :1736   Min.   :1670   Min.   :1145   Min.   : 442.0   Min.   :1069  
 1st Qu.:2590   1st Qu.:2696   1st Qu.:1588   1st Qu.: 721.0   1st Qu.:1451  
 Median :2985   Median :3148   Median :1785   Median : 848.0   Median :1622  
 Mean   :3037   Mean   :3215   Mean   :1799   Mean   : 873.1   Mean   :1607  
 3rd Qu.:3431   3rd Qu.:3686   3rd Qu.:1995   3rd Qu.:1000.0   3rd Qu.:1779  
 Max.   :4643   Max.   :5094   Max.   :2709   Max.   :1500.0   Max.   :2051  
     BSlope     
 Min.   :12.98  
 1st Qu.:21.58  
 Median :26.85  
 Mean   :26.85  
 3rd Qu.:32.05  
 Max.   :42.09  
# A tibble: 1 × 1
      n
  <int>
1  8569

plot(runGgplot(cafe2, "cafe_semisombra", factor_default))
       B2              B3              B4              B5        
 Min.   : 30.0   Min.   :162.0   Min.   : 31.0   Min.   : 435.0  
 1st Qu.:126.0   1st Qu.:312.0   1st Qu.:176.0   1st Qu.: 649.0  
 Median :165.0   Median :381.0   Median :229.0   Median : 754.0  
 Mean   :171.1   Mean   :389.9   Mean   :251.1   Mean   : 773.1  
 3rd Qu.:209.0   3rd Qu.:461.0   3rd Qu.:304.0   3rd Qu.: 888.0  
 Max.   :355.0   Max.   :691.0   Max.   :589.0   Max.   :1226.0  
       B6             B7             B8            B11            B12        
 Min.   :1476   Min.   :1808   Min.   :1735   Min.   :1129   Min.   : 409.0  
 1st Qu.:2142   1st Qu.:2666   1st Qu.:2768   1st Qu.:1552   1st Qu.: 679.0  
 Median :2421   Median :3045   Median :3205   Median :1738   Median : 788.0  
 Mean   :2490   Mean   :3113   Mean   :3282   Mean   :1758   Mean   : 816.1  
 3rd Qu.:2785   3rd Qu.:3498   3rd Qu.:3758   3rd Qu.:1953   3rd Qu.: 929.0  
 Max.   :3777   Max.   :4714   Max.   :5170   Max.   :2654   Max.   :1421.0  
      BDEM          BSlope     
 Min.   : 978   Min.   :12.94  
 1st Qu.:1271   1st Qu.:21.71  
 Median :1447   Median :26.89  
 Mean   :1436   Mean   :27.05  
 3rd Qu.:1611   3rd Qu.:32.50  
 Max.   :1841   Max.   :42.25  
# A tibble: 1 × 1
      n
  <int>
1  9005

plot(runGgplot(cafe3, "cafe_sombra", factor_default))
       B2              B3              B4              B5             B6      
 Min.   : 38.0   Min.   :156.0   Min.   : 39.0   Min.   : 401   Min.   :1499  
 1st Qu.:115.0   1st Qu.:295.0   1st Qu.:168.0   1st Qu.: 617   1st Qu.:2228  
 Median :152.0   Median :364.0   Median :213.0   Median : 717   Median :2547  
 Mean   :157.3   Mean   :370.8   Mean   :226.2   Mean   : 732   Mean   :2606  
 3rd Qu.:195.0   3rd Qu.:440.0   3rd Qu.:272.0   3rd Qu.: 836   3rd Qu.:2956  
 Max.   :307.0   Max.   :645.0   Max.   :492.0   Max.   :1169   Max.   :3913  
       B7             B8            B11            B12            BDEM     
 Min.   :1868   Min.   :1756   Min.   :1130   Min.   : 438   Min.   : 902  
 1st Qu.:2830   1st Qu.:2876   1st Qu.:1574   1st Qu.: 678   1st Qu.:1121  
 Median :3248   Median :3396   Median :1749   Median : 772   Median :1238  
 Mean   :3300   Mean   :3450   Mean   :1767   Mean   : 789   Mean   :1267  
 3rd Qu.:3744   3rd Qu.:3986   3rd Qu.:1952   3rd Qu.: 890   3rd Qu.:1401  
 Max.   :4851   Max.   :5338   Max.   :2589   Max.   :1273   Max.   :1750  
     BSlope     
 Min.   :13.82  
 1st Qu.:21.76  
 Median :26.49  
 Mean   :26.56  
 3rd Qu.:31.22  
 Max.   :41.01  
# A tibble: 1 × 1
      n
  <int>
1  9655

y para las secundarias…

plot(runGgplot(vege, "vegetacion", factor_default))
       B2              B3              B4              B5        
 Min.   : 47.0   Min.   :189.0   Min.   : 33.0   Min.   : 449.0  
 1st Qu.:174.0   1st Qu.:365.0   1st Qu.:250.0   1st Qu.: 680.0  
 Median :214.0   Median :407.0   Median :322.0   Median : 751.0  
 Mean   :212.5   Mean   :412.6   Mean   :376.5   Mean   : 778.8  
 3rd Qu.:254.0   3rd Qu.:457.0   3rd Qu.:474.0   3rd Qu.: 861.0  
 Max.   :350.0   Max.   :645.0   Max.   :896.0   Max.   :1174.0  
       B6             B7             B8            B11            B12      
 Min.   :1046   Min.   :1359   Min.   :1321   Min.   :1114   Min.   : 431  
 1st Qu.:1660   1st Qu.:2016   1st Qu.:2100   1st Qu.:1748   1st Qu.: 830  
 Median :1850   Median :2308   Median :2408   Median :1903   Median : 975  
 Mean   :1925   Mean   :2334   Mean   :2446   Mean   :1965   Mean   :1040  
 3rd Qu.:2121   3rd Qu.:2609   3rd Qu.:2742   3rd Qu.:2163   3rd Qu.:1221  
 Max.   :3288   Max.   :3755   Max.   :4067   Max.   :2913   Max.   :1814  
      BDEM            BSlope      
 Min.   :  40.0   Min.   : 0.000  
 1st Qu.:  70.0   1st Qu.: 2.582  
 Median :  95.0   Median : 4.313  
 Mean   : 203.7   Mean   : 8.718  
 3rd Qu.: 199.0   3rd Qu.:11.158  
 Max.   :1587.0   Max.   :38.250  
# A tibble: 1 × 1
      n
  <int>
1 34359

plot(runGgplot(ciud, "ciudad", factor_default))
       B2             B3             B4             B5             B6      
 Min.   : 329   Min.   : 657   Min.   : 566   Min.   :1278   Min.   :1920  
 1st Qu.: 766   1st Qu.:1104   1st Qu.:1180   1st Qu.:1695   1st Qu.:2274  
 Median :1028   Median :1370   Median :1516   Median :1895   Median :2425  
 Mean   :1039   Mean   :1384   Mean   :1515   Mean   :1905   Mean   :2431  
 3rd Qu.:1302   3rd Qu.:1654   3rd Qu.:1846   3rd Qu.:2107   3rd Qu.:2586  
 Max.   :1846   Max.   :2208   Max.   :2462   Max.   :2604   Max.   :3002  
       B7             B8            B11            B12            BDEM    
 Min.   :2069   Min.   :1982   Min.   :2072   Min.   :1576   Min.   :132  
 1st Qu.:2473   1st Qu.:2444   1st Qu.:2558   1st Qu.:2200   1st Qu.:151  
 Median :2630   Median :2654   Median :2751   Median :2468   Median :160  
 Mean   :2641   Mean   :2666   Mean   :2749   Mean   :2457   Mean   :159  
 3rd Qu.:2802   3rd Qu.:2876   3rd Qu.:2942   3rd Qu.:2725   3rd Qu.:167  
 Max.   :3318   Max.   :3484   Max.   :3450   Max.   :3220   Max.   :184  
     BSlope      
 Min.   :0.1444  
 1st Qu.:2.0031  
 Median :2.7649  
 Mean   :2.9851  
 3rd Qu.:3.9420  
 Max.   :6.8940  
# A tibble: 1 × 1
      n
  <int>
1 64782

plot(runGgplot(agua, "agua", factor_default))
       B2              B3            B4              B5            B6        
 Min.   : 89.0   Min.   :114   Min.   :158.0   Min.   :115   Min.   :  40.0  
 1st Qu.:156.0   1st Qu.:215   1st Qu.:267.0   1st Qu.:211   1st Qu.: 119.0  
 Median :171.0   Median :236   Median :356.0   Median :238   Median : 141.0  
 Mean   :185.2   Mean   :273   Mean   :347.5   Mean   :302   Mean   : 278.6  
 3rd Qu.:202.0   3rd Qu.:313   3rd Qu.:426.0   3rd Qu.:359   3rd Qu.: 181.0  
 Max.   :350.0   Max.   :631   Max.   :587.0   Max.   :983   Max.   :2024.0  
       B7             B8            B11            B12            BDEM      
 Min.   :  51   Min.   :  68   Min.   : 110   Min.   :  56   Min.   :33.00  
 1st Qu.:1127   1st Qu.: 915   1st Qu.: 630   1st Qu.: 334   1st Qu.:52.00  
 Median :2055   Median :2098   Median :1582   Median : 805   Median :57.00  
 Mean   :1822   Mean   :1846   Mean   :1326   Mean   : 722   Mean   :54.76  
 3rd Qu.:2365   3rd Qu.:2482   3rd Qu.:1950   3rd Qu.:1056   3rd Qu.:57.00  
 Max.   :3757   Max.   :3896   Max.   :2548   Max.   :1450   Max.   :65.00  
     BSlope     
 Min.   :0.000  
 1st Qu.:1.888  
 Median :3.125  
 Mean   :3.218  
 3rd Qu.:4.535  
 Max.   :8.422  
# A tibble: 1 × 1
      n
  <int>
1 18223

Comparamos los promedios entre clases principales…

ggplot(data = cafe, aes(x=band, y= value, group = id)) + 
  stat_summary(data = palma, aes(y = value, group = 1), 
               fun = mean, colour = "orange", geom = "line", group = 1) +
  stat_summary(data = cafe1, aes(y = value, group = 1), 
               fun = mean, colour = "green", geom = "line", group = 1) +
  stat_summary(data = cafe2, aes(y = value, group = 1), 
               fun = mean, colour = "blue", geom = "line", group = 1) +
  stat_summary(data = cafe3, aes(y = value, group = 1), 
               fun = mean, colour = "red", geom = "line", group = 1) +
  theme_bw()

Comparamos cafe con las clases secundarias…

ggplot(data = cafe, aes(x=band, y= value, group = id)) + 
  stat_summary(data = cafe1, aes(y = value, group = 1), 
               fun = mean, colour = "green", geom = "line", group = 1) +
  stat_summary(data = cafe2, aes(y = value, group = 1), 
               fun = mean, colour = "blue", geom = "line", group = 1) +
  stat_summary(data = cafe3, aes(y = value, group = 1), 
               fun = mean, colour = "red", geom = "line", group = 1) +
  stat_summary(data = vege, aes(y = value, group = 1), 
               fun = mean, colour = "darkgreen", geom = "line", group = 1) +
  stat_summary(data = ciud, aes(y = value, group = 1), 
               fun = mean, colour = "darkgray", geom = "line", group = 1) +
  stat_summary(data = agua, aes(y = value, group = 1), 
               fun = mean, colour = "darkblue", geom = "line", group = 1) +
  theme_bw()

Comparamos palma con las clases secundarias…

ggplot(data = palma, aes(x=band, y= value, group = id)) + 
  stat_summary(data = palma, aes(y = value, group = 1), 
               fun = mean, colour = "orange", geom = "line", group = 1) +
  stat_summary(data = vege, aes(y = value, group = 1), 
               fun = mean, colour = "darkgreen", geom = "line", group = 1) +
  stat_summary(data = ciud, aes(y = value, group = 1), 
               fun = mean, colour = "darkgray", geom = "line", group = 1) +
  stat_summary(data = agua, aes(y = value, group = 1), 
               fun = mean, colour = "darkblue", geom = "line", group = 1) +
  theme_bw()