What wrong with my code?

#1
I am running a DOE where i have generated a PB design. This ends up as a data table with my predictor variables and the experiments I need to run to generate my response variable.
Code:
library(Rcmdr)
library(RcmdrMisc)
library(RcmdrPlugin.DoE)
PB.DOE <- pb(nruns= 12 ,n12.taguchi= FALSE ,nfactors= 12 -1, ncenter= 0 ,
            replications= 1 ,repeat.only= FALSE ,randomize= TRUE ,seed= 27241 , 
            factor.names=list( A=c(100,1000),B=c(100,200),C=c(1,3),D=c(1,1.7),
            E=c(1000,1500),F=c(-2,2) ) )
I have a calculated column called IP using the variables from the design generated.
Code:
IP <- with(PB.DOE,(as.numeric(A)*as.numeric(B)*(5000-3000))/(141.2*as.numeric(C)*as.numeric(D)*(log10(as.numeric(E)/0.25)-(1/2)+as.numeric(F))))
I then want to add that column to my original table.
Code:
final_set <- within(PB.DOE, {
IP<- ((as.numeric(A)*as.numeric(B)*(5000-3000))/(141.2*as.numeric(C)*as.numeric(D)*(log10(as.numeric(E)/0.25)-(1/2)+as.numeric(F))))
})
The problem I am facing is that the values calculated for my IP column are not right (should compute according to the formula) and I don't know why?

Is there something wrong with the code?
 
Last edited:

trinker

ggplot2orBust
#2
This post is interesting but is not minimally working. When I grab your code and run it I get:

Code:
> PB.DOE <- pb(nruns= 12 ,n12.taguchi= FALSE ,nfactors= 12 -1, ncenter= 0 ,
+             replications= 1 ,repeat.only= FALSE ,randomize= TRUE ,seed= 27241 , 
+             factor.names=list( A=c(100,1000),B=c(100,200),C=c(1,3),D=c(1,1.7),
+             E=c(1000,1500),F=c(-2,2) ) )
Error: could not find function "pb"
There's a nice section in here (http://www.talkstats.com/showthread.php/29338-How-to-use-Code-tags) about making a minimally reproducible example.
 
#3
This post is interesting but is not minimally working. When I grab your code and run it I get:

Code:
> PB.DOE <- pb(nruns= 12 ,n12.taguchi= FALSE ,nfactors= 12 -1, ncenter= 0 ,
+             replications= 1 ,repeat.only= FALSE ,randomize= TRUE ,seed= 27241 , 
+             factor.names=list( A=c(100,1000),B=c(100,200),C=c(1,3),D=c(1,1.7),
+             E=c(1000,1500),F=c(-2,2) ) )
Error: could not find function "pb"
There's a nice section in here (http://www.talkstats.com/showthread.php/29338-How-to-use-Code-tags) about making a minimally reproducible example.
Hi. I forgot to mention that i am using the Package RcmdrPlugin.DoE (which needs to be active and is a plugin to the r commander rcmdr) that is why you get the error could not find "pb"
 
#4
This post is interesting but is not minimally working. When I grab your code and run it I get:

Code:
> PB.DOE <- pb(nruns= 12 ,n12.taguchi= FALSE ,nfactors= 12 -1, ncenter= 0 ,
+             replications= 1 ,repeat.only= FALSE ,randomize= TRUE ,seed= 27241 , 
+             factor.names=list( A=c(100,1000),B=c(100,200),C=c(1,3),D=c(1,1.7),
+             E=c(1000,1500),F=c(-2,2) ) )
Error: could not find function "pb"
There's a nice section in here (http://www.talkstats.com/showthread.php/29338-How-to-use-Code-tags) about making a minimally reproducible example.
yeah. I tried too :)
 

trinker

ggplot2orBust
#5
anonake said:
Hi. I forgot to mention that i am using the Package RcmdrPlugin.DoE (which needs to be active and is a plugin to the r commander rcmdr) that is why you get the error could not find "pb"
Can you reformat your original post and add library calls and data so we can run your code?
 
#6
I have edited the code with the library calls in the original post. This is the full code:
Code:
library(Rcmdr)
library(RcmdrMisc)
library(RcmdrPlugin.DoE)

PB.DOE <- pb(nruns= 12 ,n12.taguchi= FALSE ,nfactors= 12 -1, ncenter= 0 ,
             replications= 1 ,repeat.only= FALSE ,randomize= TRUE ,seed= 27241 , 
             factor.names=list( A=c(100,1000),B=c(100,200),C=c(1,3),D=c(1,1.7),
                                E=c(1000,1500),F=c(-2,2) ) )

IP <- with(PB.DOE,(as.numeric(A)*as.numeric(B)*(5000-3000))/(141.2*as.numeric(C)*as.numeric(D)*(log10(as.numeric(E)/0.25)-(1/2)+as.numeric(F))))


final_set <- within(PB.DOE, {
  IP<- ((as.numeric(A)*as.numeric(B)*(5000-3000))/(141.2*as.numeric(C)*as.numeric(D)*(log10(as.numeric(E)/0.25)-(1/2)+as.numeric(F))))
})

final_set
 

trinker

ggplot2orBust
#7
Perfect. Can I ask if you expect 1108187 for the first row's IP value? Below I manually inserted the values. I want to confirm this is correct before moving forward.


Code:
((as.numeric(1000)*as.numeric(100)*(5000-3000))/(141.2*as.numeric(1)*as.numeric(1)*(log10(as.numeric(1500)/0.25)-(1/2)+as.numeric(-2))))

1108187
 
#8
Perfect. Can I ask if you expect 1108187 for the first row's IP value? Below I manually inserted the values. I want to confirm this is correct before moving forward.


Code:
((as.numeric(1000)*as.numeric(100)*(5000-3000))/(141.2*as.numeric(1)*as.numeric(1)*(log10(as.numeric(1500)/0.25)-(1/2)+as.numeric(-2))))

1108187
The first row should evaluate to 228474 based on this formula:

(A*B*(5000-3000))/(141.2*C*D*(ln(E/0.25)-(1/2)-2))

and this table:

A B C D E F e1 e2 e3 e4 e5
1 1000 100 1 1 1500 -2 1 1 -1 1 1
2 1000 100 3 1.7 1000 2 1 1 -1 -1 -1
3 100 100 1 1.7 1000 2 1 -1 1 1 1
4 1000 200 1 1 1000 2 -1 1 1 -1 1
5 1000 100 3 1.7 1500 -2 -1 -1 1 -1 1
6 100 200 1 1.7 1500 -2 1 1 1 -1 -1
7 100 200 3 1 1500 2 1 -1 -1 -1 1
8 100 100 1 1 1000 -2 -1 -1 -1 -1 -1
9 100 200 3 1.7 1000 -2 -1 1 -1 1 1
10 100 100 3 1 1500 2 -1 1 1 1 -1
11 1000 200 1 1.7 1500 2 -1 -1 -1 1 -1
12 1000 200 3 1 1000 -2 1 -1 1 1 -1
 

trinker

ggplot2orBust
#9
OK I'm not familiar with this pb data storage but using str on it shows that it stores the numbers as factors. You're wrapping with as.numeric and factors are in a sense already numbers not the character you supposed them to be. Here's what I mean:

Code:
as.numeric(as.factor(100))
## [1] 1
You get 1 instead of 100 as you might expect. One way to get at what you're after is to wrap with as.character first:

Code:
as.numeric(as.character(as.factor(100)))
## [1] 100
So for your problem we'll make an as.numeric2 function that first wraps with as.character:

Code:
 as.numeric2 <- function(x) as.numeric(as.character(x))
 
IP <- with(PB.DOE,(as.numeric2(A)*as.numeric2(B)*(5000-3000))/(141.2*as.numeric2(C)*as.numeric2(D)*(log(as.numeric2(E)/0.25)-(1/2)+as.numeric2(F))))
IP
## [1] 228474.429  28357.166   8507.150 289243.091  44798.908  26879.345
## [7]   9258.157  24446.297   9586.783   4629.078 163379.237 162975.315
Note originally you said you wanted log10 but later indicated the natural log which matches your exception.
 
#10
OK I'm not familiar with this pb data storage but using str on it shows that it stores the numbers as factors. You're wrapping with as.numeric and factors are in a sense already numbers not the character you supposed them to be. Here's what I mean:

Code:
as.numeric(as.factor(100))
## [1] 1
You get 1 instead of 100 as you might expect. One way to get at what you're after is to wrap with as.character first:

Code:
as.numeric(as.character(as.factor(100)))
## [1] 100
So for your problem we'll make an as.numeric2 function that first wraps with as.character:

Code:
 as.numeric2 <- function(x) as.numeric(as.character(x))
 
IP <- with(PB.DOE,(as.numeric2(A)*as.numeric2(B)*(5000-3000))/(141.2*as.numeric2(C)*as.numeric2(D)*(log(as.numeric2(E)/0.25)-(1/2)+as.numeric2(F))))
IP
## [1] 228474.429  28357.166   8507.150 289243.091  44798.908  26879.345
## [7]   9258.157  24446.297   9586.783   4629.078 163379.237 162975.315
Note originally you said you wanted log10 but later indicated the natural log which matches your exception.
Thanks so much!!!