多元回归线的一般方程

时间:2014-03-30 20:28:20

标签: r statistics regression nls

R中是否有办法找出' general'图中的多行方程?下图是我想要实现的一个例子。我只展示了t的主要线条,但在这些主线之间也有隐藏的线条。我想要的是获得一般性的'线的等式。我该怎么做呢?

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"))

1 个答案:

答案 0 :(得分:1)

我不知道任何自动模型拟合,但在您的情况下,enter image description hereenter image description here之间的关系似乎是enter image description here。因此,您可以为每个enter image description here计算每个enter image description hereenter image description here,以便找到“一般的eqausion”

根据您的数据集

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))

计算每个enter image description here

的估计值和s.e的函数
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])  
}

运行功能

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

现在你可以估计每一行。例如,估算enter image description here并比较

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