In r, get output values in power curve for 'a' and 'b' values

淺唱寂寞╮ 提交于 2019-12-20 04:36:54

问题


I apologise for this basic question but i am truly stuck for whatever reason. I am hoping to get the output values from a power curve of 'a' and 'b' from y = a*x^b. Assuming i have this data set:

 x       y       log10(x)         log10(y)
 7      240     0.84509804      2.380211242
45      610     1.653212514     2.785329835
14      340     1.146128036     2.531478917
30      500     1.477121255     2.698970004
24      450     1.380211242     2.653212514
57      690     1.755874856     2.838849091

And it produces this graph in excel:

Using the excel example, how would i get the output values in r for a=2.465 and b=0.2401.


回答1:


Your data:

DF <- read.table(text="x       y       log10(x)         log10(y)
7      240     0.84509804      2.380211242
45      610     1.653212514     2.785329835
14      340     1.146128036     2.531478917
30      500     1.477121255     2.698970004
24      450     1.380211242     2.653212514
57      690     1.755874856     2.838849091", header=TRUE)

Create a selfstarting model: (Disclaimer: The function names doesn't reflect my political attitude, it's merely in line with the naming scheme of selfstarting functions.)

SSpower <- selfStart(~ A*x^B,
                     function(mCall, data, LHS)
                     {
                       xy <- sortedXyData(mCall[["x"]], LHS, data)
                       if(nrow(xy) < 3) {
                         stop("Too few distinct x values to fit a power function")
                       }
                       z <- xy[["y"]]
                       xy[["logx"]] <- log(xy[["x"]])     
                       xy[["logy"]] <- log(xy[["y"]])  
                       aux <- coef(lm(logy ~ logx, xy))
                       pars <- c(exp(aux[[1]]), aux[[2]])
                       setNames(pars,
                                mCall[c("A", "B")])
                     }, c("A", "B"))

Use the selfstarting model for fitting:

fit <- nls(y ~ SSpower(x, A, B), data=DF)

Plot:

plot(y~x, data=DF)
lines(x=1:60, y=predict(fit, newdat=list(x=1:60)))




回答2:


Have a look at the following (and make your problem reproducible next time. That wil probably also help you to point at the precise problem):

x  <-  c(7, 45, 14, 30, 24, 57)
y  <-  c(240, 610, 340, 500, 450, 690)

logx  <-  log10(x)
logy  <-  log10(y)

lm.000  <-  lm(logy ~ logx)
summary(lm.000)

coef(lm.000)[1]
coef(lm.000)[2]

plot(x, y)
curve(x^coef(lm.000)[2]*10^coef(lm.000)[1], add=TRUE, lwd=2)



回答3:


data is your dataset:

y=data[,3]
x= data[,4]

nls(x ~ A * y ^ B)
Nonlinear regression model
  model: x ~ A * y^B
   data: parent.frame()
     A      B 
2.4635 0.2421 
 residual sum-of-squares: 0.0008733

Number of iterations to convergence: 5 
Achieved convergence tolerance: 6.774e-06


来源:https://stackoverflow.com/questions/19375182/in-r-get-output-values-in-power-curve-for-a-and-b-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!