使用predict()或使用显式拟合方程计算的y值之间的差异

时间:2019-06-11 10:27:37

标签: r parameters interpolation data-fitting

如果我使用predict()函数计算特定x值的y值,则得到的值与我可以使用显式拟合方程计算的值不同。

我使用nls(MyEquation)拟合了以下数据,并获得了m1,m2,...参数。 然后,我想使用predict(m)函数或我用于拟合的显式方程(输入所需的x值)来反向计算特定x值的y值。 对于相同的x值,我获得了不同的y值。哪个是正确的?

> df
    pH activity
1  3.0     0.88
2  4.0     1.90
3  5.0    19.30
4  6.0    70.32
5  7.0   100.40
6  7.5   100.00
7  8.0    79.80
8  9.0     7.75
9 10.0     1.21

x <- df$pH
y <- df$activity
m<-nls(y~(m1*(10^(-x))+m2*10^(-m3))/(10^(-m3)+10^(-x)) - (m5*(10^(-x))+1*10^(-i))/(10^(-i)+10^(-x)), start = list(m1=1,m2=100,m3=7,m5=1))

> m
Nonlinear regression model
  model: y ~ (m1 * (10^(-x)) + m2 * 10^(-m3))/(10^(-m3) + 10^(-x)) - (m5 *     (10^(-x)) + 1 * 10^(-i))/(10^(-i) + 10^(-x))
   data: parent.frame()
      m1       m2       m3       m5 
-176.032   13.042    6.282 -180.704 
 residual sum-of-squares: 1522

Number of iterations to convergence: 14 
Achieved convergence tolerance: 5.805e-06

list2env(as.list(coef(m)), .GlobalEnv)

#calculate y based on fitting parameters
# choose the 7th x value (i.e. x[7]) that corresponds to pH = 8
# (using predict)
> x_pH8 <- x[7]
> predict(m)[7]
[1] 52.14299

# (using the explicit fitting equation with the fitted parameters
> x1 <- x_pH8
> (m1*(10^(-x1))+m2*10^(-m3))/(10^(-m3)+10^(-x1)) - (m5*(10^(-x1))+1*10^(-8.3))/(10^(-8.3)+10^(-x1))
[1] 129.5284

如您所见: 预测(m)[7]得出y = 52.14299(对于x = 8)

同时

(m1 *(10 ^(-x1))+ m2 * 10 ^(-m3))/(10 ^(-m3)+10 ^(-x1))-(m5 *(10 ^(-x1) ))+ 1 * 10 ^(-8.3))/(10 ^(-8.3)+10 ^(-x1))得出y = 129.5284(对于x = 8)

1 个答案:

答案 0 :(得分:0)

您在手动计算中使用的package hr.atos.praksa.josipmaricevic.zadatak15; import java.util.Scanner; import java.util.regex.Pattern; public class Functions { static Scanner input = new Scanner(System.in); final static private Pattern patternNotBlank = Pattern.compile("\\S+"); final static private Pattern patternOIB = Pattern.compile("^[0-9]{11}$"); final static private Pattern patternString = Pattern.compile("[A-Z][a-z]*"); final static private Pattern patternInt = Pattern.compile("[0-9]+$"); final static private Pattern patternFloat = Pattern.compile("^[0-9]+([.]?[0-9]+|[0-9]*)"); public static String inputCheck(String print, String _pattern, boolean andStatement, boolean orStatement ) { String string = ""; Pattern pattern = patternFloat; switch(_pattern) { case "int": pattern = patternInt; break; case "float": pattern = patternFloat; break; case "string": pattern = patternString; break; case "oib": pattern = patternOIB; break; case "notblank": pattern = patternNotBlank; break; } while((andStatement && pattern.matcher(string).matches()==false) || orStatement){ System.out.print(print); string = input.nextLine(); } return string; } } 的值可能与模型拟合中使用的值不同。我没有任何差异:

i