Domanda

Is there a way in R to find out the 'general' equation of multiple lines in a graph? The following graph is an example of what I wanted to achieve. I have only shown the major lines for t but there are also hidden lines in between those major lines. What I want is to get the 'general' equation for the lines. How do I do this?

library(reshape2)
library(ggplot2)

a<-data.frame(c =c(0.01,0.02,0.03,0.05,0.07,0.10,0.15,0.20,0.30,0.50,0.70,1),
    t1 =c(5,5.55,5.94,6.53,6.98,7.54,8.29,8.90,9.92,11.51,12.80,14.46),
    t2 = c(4.35,4.84,5.19,5.72,6.12,6.62,7.29,7.84,8.74,10.16,11.33,12.80),
    t3 = c(3.70, 4.13,4.44,4.89,5.25,5.68,6.26,6.73,7.52,8.74, 9.74,10.99),
    t4 = c(3.08,3.45,3.70,4.09,4.39,4.75,5.23,5.63,6.28,7.28,8.09,9.11),
    t5 = c(2.51,2.80,3.01,3.33,3.57,3.86,4.25,4.56,5.07,5.85,6.54,7.19))

b<-melt(a, id = "c")

ggplot(b,aes(x = c, y = value, color = variable)) +
     geom_line() + scale_color_discrete(name= expression('t'), labels=c("t = 1", "t = 2", "t = 3", "t = 4","t = 5"))
È stato utile?

Soluzione

I don't know of any automatic model fitting, but it seems like in your case the relationship between enter image description here and enter image description here is enter image description here. Thus, you could calculate each enter image description here and enter image description here for each enter image description here in order to find the 'general eqausion'

So per your data set

a <- data.frame(c =c(0.01,0.02,0.03,0.05,0.07,0.10,0.15,0.20,0.30,0.50,0.70,1),
              t1 =c(5,5.55,5.94,6.53,6.98,7.54,8.29,8.90,9.92,11.51,12.80,14.46),
              t2 = c(4.35,4.84,5.19,5.72,6.12,6.62,7.29,7.84,8.74,10.16,11.33,12.80),
              t3 = c(3.70, 4.13,4.44,4.89,5.25,5.68,6.26,6.73,7.52,8.74, 9.74,10.99),
              t4 = c(3.08,3.45,3.70,4.09,4.39,4.75,5.23,5.63,6.28,7.28,8.09,9.11),
              t5 = c(2.51,2.80,3.01,3.33,3.57,3.86,4.25,4.56,5.07,5.85,6.54,7.19))

A function that calculates estimates and s.e for each enter image description here

ResFunc <- function(x) {
  temp <- lm(reformulate("c", response = x), log(a))
  c(exp(coef(temp)[[1]]), coef(temp)[[2]], exp(summary(temp)$coefficients[1,2]), summary(temp)$coefficients[2,2])  
}

Running the function

temp <- as.data.frame(t(sapply(setdiff(names(a),"c"), ResFunc)))
colnames(temp) <- c("a", "b", "S.E (a)", "S.E (b)")
temp

#           a         b  S.E (a)     S.E (b)
#t1 13.422867 0.2314997 1.024901 0.009622679
#t2 11.888155 0.2353401 1.024803 0.009585284
#t3 10.237551 0.2375002 1.024013 0.009283321
#t4  8.523443 0.2366266 1.022568 0.008730912
#t5  6.831186 0.2321247 1.020344 0.007879240

Now you could potentially estimate each line. For example, estimating enter image description here and comparing

b <- data.frame(c = a$c, t1 = a$t1, t1.est = temp[1,1]*(a$c^temp[1,2]))
test <- melt(b, "c")
ggplot(test, aes(x = c, y = value, color = variable)) + geom_line() 

enter image description here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top