2016-03-17 19 views
5

Dopo la selezione del modello, il mio modello migliore ha due variabili continue con un termine di interazione.Arresto dell'estrapolazione quando si prevede di utilizzare due variabili

miei dati assomiglia a questo:

df<-structure(list(Height_RR = c(1.09861228866811, -0.143100843640673, 
          -0.990398704027877, -0.990398704027877, 0.0800427076735365, 0.916290731874155, 
          1.04982212449868, 0.430782916092454, 0.575364144903562, 0.0953101798043248, 
          -0.405465108108164, 0.132489183046079, 0.117783035656383, 1.04982212449868, 
          -0.916290731874155, 1.20397280432594, -0.356674943938732, 0.287682072451781, 
          -0.356674943938732, 0.287682072451781, -0.356674943938732, 0.287682072451781, 
          -0.318387168583869, -0.318387168583869, -0.318387168583869, -0.356674943938732, 
          -0.393042588109607, -0.485507815781701, -0.405465108108164, -0.441832752279039, 
          0.42744401482694, 0.510825623765991, 1.04982212449868, 0.847297860387204, 
          -0.356674943938732), 
       CWD = c(-173.850331373113, -1063.65743501896, 
       -1152.77456298827, -1200.14062275391, -1191.1454796875, -154.366681649412, 
       -160.237513231871, -148.917478721144, -655.155005728001, -1081.17124999999, 
       -192.437500000001, -664.538976252586, -540.755930464004, -230.945841787738, 
       -632.705104201392, -159.376784299775, -686.792144988283, -686.792144988283, 
       -686.792144988283, -686.792144988283, -686.792144988283, -686.792144988283, 
       -681.113600988285, -681.113600988285, -681.113600988285, -203.026923561875, 
       -32.6625046554887, -499.426709860029, -190.121159505859, -871.942483370128, 
       -988.569593505865, -248.140000031999, -165.422048285183, -165.422048285183, 
       -165.422048285183), 
       yi = c(0.201673327454905, 0.374187870755073, 
       -1.26459742614237, -3.4162352590267, -1.20397280432594, 0.943371690476826, 
       1.26291533995939, 0.551099522903931, 0.506212624114382, 1.62113395219729, 
       -0.272257602126149, 0.345151698858396, 0.104290175606367, 4.56284362374715, 
       0.532233017860013, 0.697137826765664, -1.01160091167848, -1.07044141170141, 
       0.646627164925052, -0.519875459285909, -0.646627164925052, -0.519875459285909, 
       -0.424883193965266, 0.074107972153722, 0.074107972153722, 0.693147180559945, 
       0.0707340508073709, 0.358554516423803, -0.682820066404096, -0.638455438611528, 
       0.410070448690588, 1.11317243999944, 1.26976054486394, 1.45111186119636, 
       1.44238382777093)), .Names = c("Height_RR", "CWD", "yi"), row.names = c(1L, 
       2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
       17L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
       31L, 33L, 34L, 35L, 36L, 37L, 38L), class = "data.frame") 

E il mio modello stime dei parametri simile a questa:

    estimate se  zval pval ci.lb  ci.ub  
intrcpt   1.1422 0.3862 2.9575 0.0031 0.3852 1.8991 ** 
Height_RR  0.3412 0.1488 2.2925 0.0219 0.0495 0.6328 * 
CWD    0.0014 0.0006 2.2698 0.0232 0.0002 0.0026 * 
Height_RR:CWD -0.0006 0.0002 -3.0161 0.0026 -0.0009 -0.0002 ** 

voglio visualizzare le previsioni di questo risultato utilizzando una figura in cui entrambi gli assi xey sono le variabili predittive continue e il valore previsto è rappresentato da una rampa di colori. Utilizzando questo codice, ottengo il seguente figura

library(ggplot2) 
new.data<-expand.grid(Height_RR=seq(min(df$Height_RR),max(df$Height_RR),length.out = 20), 
        CWD=seq(min(df$CWD,na.rm = T),max(df$CWD,na.rm = T),length.out = 20)) 

new.data$yi<-(new.data$Height_RR*0.3412)+1.1422+(0.0014*new.data$CWD)+((new.data$CWD*new.data$Height_RR)*0.0006) 


ggplot(new.data,aes(x=Height_RR,y=CWD,fill=yi))+geom_raster()+scale_fill_continuous(low="light grey",high="black") 

enter image description here

Tuttavia, non ho i dati per tutte le regioni di questo complotto e quindi sto estrapolando per questo modello. Il guscio convesso minimo e punti sulla figura seguente mostra dove il mio trattamento è

enter image description here

Le mie domande sono:

  1. C'è un modo per creare un dataframe per limitare le mie previsioni per la regione Ho dati per (cioè all'interno del poligono)?
  2. In caso contrario, c'è un modo migliore per visualizzare le interazioni tra due variabili continue ?

risposta

4

Ecco un'idea, spero capito bene ..

# Create a convex hull of the points in df and turn that into a polygon of class SpatialPolygons: 

    ch <- chull(df$Height_RR, df$CWD) 
    poly.df <- df[c(ch, ch[1]),] 
    poly <- SpatialPolygons(list(Polygons(list(Polygon(as.matrix(poly.df[,1:2]))),1))) 


# Create a SpatialPointsDataFrame with new.data: 

    library(sp) 
    new.data.poly <- new.data 
    coordinates(new.data.poly) <- ~Height_RR+CWD 

# Extract the points in new.data which are covered by the polygon: 

    new.data$inp <- over(new.data.poly, poly) 
    new.data <- new.data[complete.cases(new.data),] 

# Calculate yi as you did: 

    new.data$yi<-(new.data$Height_RR*0.3412) + 1.1422 + (0.0014*new.data$CWD) + ((new.data$CWD*new.data$Height_RR)*0.0006) 

# Plot:  
    ggplot(new.data, aes(x=Height_RR,y=CWD,fill=yi)) + 
    geom_raster() + 
    scale_fill_continuous(low="light grey",high="black") 

enter image description here

+1

Sì, è esattamente quello che volevo. Grazie mille! –

0

Si potrebbe utilizzare Dismo :: convHull per stimare e prevedere da (determinare se i dati sono a) un convesso. Potresti quindi usarlo per mascherare le celle che cadono fuori dallo scafo nella tua trama corrente.

+0

Come dovrei creare la maschera però? Penso che sia quello con cui sto combattendo qui. –

0

Stai cercando qualcosa di simile?

ggplot(df,aes(x=Height_RR,y=CWD))+geom_point(aes(col=yi)) 

Plot result

+0

Non proprio. Voglio che il geom_raster sia limitato all'area che si sovrappone al mio minimo scafo convesso. Se potessi creare una maschera per rimuovere tutto ciò che cade al di fuori del poligono, allora sarebbe perfetto. –

Problemi correlati