zzbb2266 - 8 months ago 29

R Question

Sorry about this stupid question guys... but seems that I really need some help with this part of the new project:

`k1 = 1e-6`

k2 = 2e-7

k3 = 1e-10

k4 = 1e-12

k5 = 1e-12

k6 = 3e-14

k7 = 4e-12

k8 = 5e-15

k9 = 6e-12

k10 = 7e-13

k11 = 1e-11

k12 = 1e-12

data2 <- read.csv

t = data2$XA*10000

B = 1E-6

A = B*t^(3/2)

R0 = 1e-5

e = exp(1)

AR1 = (k1*R0*e^-((k1+k2)*t)+k2*R0)/(k1+k2)

ARI = (k3*AR*e^-((k3+k4)*t+k4*AR))/(k3+k4)

RI = (k5*ARI*e^-((k5+k6)*t+k6*ARI))/(k5+k6)

R = (k7*RI*e^-((k7+k8)*t+k8*RI))/(k7+k8)

AR2 = (k9*R*e^-((k9+k10)*t+k10*R))/(k9+k10)

AR = AR1 + AR2

Effect = (k11*AR*e^-((k11+k12)*t+k12*ARI))/(k11+k12)

plot(t, Effect)

Then it turns out this result:

`Error in eval(expr, envir, enclos) : object 'AR' not found`

Actually it's a part of multi-phase chemical reaction chain simulation. Is that the problem of the lacking of the if...else statement? Sorry I'm totally new in R right now. Thanks.

Here is the data set FYI

`XA`

0

1.00E-10

5.00E-09

9.90E-09

1.48E-08

1.97E-08

2.46E-08

2.95E-08

3.44E-08

3.93E-08

9.00E-04

1.00E-03

2.00E-03

3.00E-03

4.00E-03

5.00E-03

6.00E-03

7.00E-03

8.00E-03

9.00E-03

1.00E-02

Answer

There's no possible ordering of rows that isn't circular logic.

`R`

is in the definition of `AR2`

but the definition of `R`

depends on `RI`

which depends on `ARI`

and thus `AR`

and thus `AR2`

. So it's circular.

Let's assume that one of these is a mistake. Since there's not enough information for me to know which is the mistake I will assume `RI`

should not be in the definition of `R`

.

By fixing the circular logic we now have working code:

```
k1 = 1e-6
k2 = 2e-7
k3 = 1e-10
k4 = 1e-12
k5 = 1e-12
k6 = 3e-14
k7 = 4e-12
k8 = 5e-15
k9 = 6e-12
k10 = 7e-13
k11 = 1e-11
k12 = 1e-12
data("mtcars")
data2 <- mtcars
t = data2$mpg*10000
B = 1E-6
A = B*t^(3/2)
R0 = 1e-5
e = exp(1)
R = (k7*e^-((k7+k8)*t+k8))/(k7+k8) #*RI
AR1 = (k1*R0*e^-((k1+k2)*t)+k2*R0)/(k1+k2)
AR2 = (k9*R*e^-((k9+k10)*t+k10*R))/(k9+k10)
AR = AR1 + AR2
ARI = (k3*AR*e^-((k3+k4)*t+k4*AR))/(k3+k4)
RI = (k5*ARI*e^-((k5+k6)*t+k6*ARI))/(k5+k6)
Effect = (k11*AR*e^-((k11+k12)*t+k12*ARI))/(k11+k12)
plot(t, Effect)
```

**Update**

I saw your edit where you added `data2`

-- thanks. Here's the solution using your actual data:

```
XA
0
1.00E-10
5.00E-09
9.90E-09
1.48E-08
1.97E-08
2.46E-08
2.95E-08
3.44E-08
3.93E-08
9.00E-04
1.00E-03
2.00E-03
3.00E-03
4.00E-03
5.00E-03
6.00E-03
7.00E-03
8.00E-03
9.00E-03
1.00E-02
data2 <- read.table(con<-file("clipboard"),header=T)
k1 = 1e-6
k2 = 2e-7
k3 = 1e-10
k4 = 1e-12
k5 = 1e-12
k6 = 3e-14
k7 = 4e-12
k8 = 5e-15
k9 = 6e-12
k10 = 7e-13
k11 = 1e-11
k12 = 1e-12
t = data2$XA*10000
B = 1E-6
A = B*t^(3/2)
R0 = 1e-5
e = exp(1)
R = (k7*e^-((k7+k8)*t+k8))/(k7+k8) #*RI
AR1 = (k1*R0*e^-((k1+k2)*t)+k2*R0)/(k1+k2)
AR2 = (k9*R*e^-((k9+k10)*t+k10*R))/(k9+k10)
AR = AR1 + AR2
ARI = (k3*AR*e^-((k3+k4)*t+k4*AR))/(k3+k4)
RI = (k5*ARI*e^-((k5+k6)*t+k6*ARI))/(k5+k6)
Effect = (k11*AR*e^-((k11+k12)*t+k12*ARI))/(k11+k12)
plot(t, Effect)
```