0

I've got a data frame that looks like this.

a[,2:25]
     UT1 UT2 UT3 UT4 UT5 UT6 UT7 UT8 UT9 UT10 UT11 UT12 TR1 TR2 TR3 TR4
3094   9   0   1  37   6   2   8   1   1    6    3    1   3   0   0   1
3095   4   0   0  10  17   6   7   1   5    3    1   12   2   0   0   1
3096  18   0   0   4   6  15  14   0   7    9    3    8   5   2   1   2
3097  11   0   0   7   5  15  10   2   4    7   16   17   7   3   0   0
3098  18   0  11   2   5  11   7   3   2    1    1    0   3   3   1   1
3099  25   0   6  11  17   3  10   1   1    3    9    2   2   1   1   2
3100   1   0   1  27  12  28  27   0   2   11    6    0   1   7   4   6
3101   0   0   1  40   0  17  13   1   0    3    3    0   1   3   3   1
3102   2   0   0  30   1   9   2   1   1    5    0    0   1   3   3   0
3103   3   0   0  11   4   7   5   2   4    0    1    0   5   4   0   0
3104   5   0   0   3   1  10   4   2   3    0    3    0   7   2   1   0
     TR5 TR6 TR7 TR8 TR9 TR10 TR11 TR12
3094   1   0  15   3   0    0   42    1
3095   1   0   4  29   0    0   42    0
3096   0   0   3  22   0    0    3    0
3097   1   0   4  14   0    0    2    0
3098   0   0   1  10   0    0    1    0
3099   0   0   4  41   1    0    3    0
3100   0   0  10  21   0    0   17    0
3101   0   0   2   1   1    0   13    3
3102   0   0   2   4   0    0   10    3
3103   1   0   3   4   0    0   12    1
3104   0   0   1   2   0    0    8    0

The first column of my data it's time so I separated it using

tiempo<-a$Tiempo
tiempo
 [1] 618.6 618.8 619.0 619.2 619.4 619.6 619.8 620.0 620.2 620.4 620.6 

In order to plot each column as a fucntion of time and do lm I used reshape package and lattice. I'm not sure that's the best option but almost gets me what I want. The code looks like this:

m<-melt(a[,2:25])
f<-m$variable
xyplot(m$value~tiempo | f, panel=function(x,y,...){
  panel.xyplot(x,y,...)
  panel.lmline(x,y, col=2, lty=2)
})

And the output is this graph

enter image description here

I don't get why it gives this error, I expect them to be non-NA, I don't understand why it is a problem. In fact, the first panel worked just fine.

When I change the panel.lmline(...) part this happens:

xyplot(m$value~tiempo | f, panel=function(x,y,...){
  panel.xyplot(tiempo,m$value,...)
  panel.lmline(tiempo,m$value, col=2, lty=2)
})

enter image description here

I get this lenght error but I think it's because each panel is using all datapoints from m when it should be using only 11.

The lm regression function I use is separated from the plotting and this doesn't mess with my statistical analysis but I'm trying to put everything together and won't be able to do it if I can't plot the data. I want visual information about the regression in order to be able to remove outliers if the Rsquared is too low or maybe not even consider that observation.

I hope I've made myself clear. Thank you very much

Edited with suggestions

enter image description here

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
Matias Andina
  • 4,029
  • 4
  • 26
  • 58
  • If you just do `xyplot(m$value~tiempo | f)` do you get the expected plot, minus the lmline? – John Paul May 01 '14 at 12:54
  • You need to reshape with the time variable. Lengths need to match up. Do this `a.melt <-melt(a, id.var="tiempo")` – infominer May 01 '14 at 13:01
  • @infominer when I use xyplot(a.melt$value~a.melt$Tiempo | f) I get all the different panles but I get "extra values" plotted. I don't know what extra information is plotting. I edited the question for you to see it – Matias Andina May 01 '14 at 14:37
  • @JohnPaul I don't get the correct plot doing that. It looks like the first one but without the lm – Matias Andina May 01 '14 at 14:39
  • @MatiasAndina just do `xyplot(value ~ Tiempo | variable, data = a.melt, ....)` Replace the ... with the panel functions you defined in your Question. – infominer May 01 '14 at 14:41
  • @infominer that worked fine! why don't you answer so I can give you some credit? – Matias Andina May 01 '14 at 15:38

1 Answers1

0

You got most of the code right.

It would be better to use the time (tiempo) variable as an id variable in your melt call This will ensure the lengths of the data match up.

library(reshape2) #This is faster version of reshape
df.m <- melt(df.matias, id.var="Tiempo") #I stored your data in df.matias

Now we can use the melted data to make your plot

library(lattice)
xyplot(value ~ Tiempo | variable, data = df.m,
   panel = function(x,y,...) {
     panel.xyplot(x,y,...)
     panel.lmline(x,y, col = 2, lty =2)
})
infominer
  • 1,981
  • 13
  • 17