ElMaestro ★★★ Denmark, 2018-01-23 00:49 (2513 d 12:47 ago) Posting: # 18254 Views: 6,046 |
|
Hi all, there is something about R internals that I don 't understand and I can best exemplify it using some code from power.TOST but my question really isn't about anything in that package. Imagine I do this in R with powerTOST: sampleN.RSABE(CV=0.3, theta0=0.95) Nice. Now, assume I don't want all that intermediate mumbojumbo, and let us for the time being completely forget the print/verbose/details option so I just want the sample size itself as a numeric. If I inspect the code I see the last few lines: res <- data.frame(design = design, alpha = alpha, CVwT = CVwT, ...and therefore I can simply do this: sampleN.RSABE(CV=0.3, theta0=0.95)$n (the n column is baptised as "Sample size" but is still accessible via $n) If I look at the corresponding code snippet from sampleN.TOST we have: res <- data.frame(design = design, alpha = alpha, CV = CV, But if I go sampleN.TOST(CV=0.3, theta0=0.95)$n then I get NULL. What determines that difference in behaviour? Why does the res data.frame from both the two functions not hold an extractable $n ? Note, this really is solely about R behaviour and R function return/internals (if I want the sample sizes I can also easily get them via [1,7] or [1,8] from the sample size object returned; all this isn't really what this question is about). — Pass or fail! ElMaestro |
Helmut ★★★ Vienna, Austria, 2018-01-23 14:37 (2512 d 23:00 ago) @ ElMaestro Posting: # 18268 Views: 5,140 |
|
Hi ElMaestro, that’s strange. Let’s try to access all values by their (original) names. library(PowerTOST) We get some NULLs also from sample.RSABE() . Even more interesting calling a variable which does not exist (since R is case-sensitive). foo()$design gave NULL, but:sampleN.RSABE(CV=0.3, theta0=0.95)$Design In the functions of PowerTOST design is character variable, but Design is a 1-level factor:is.factor(sampleN.TOST(CV=0.3, theta0=0.95)$Design) I don’t get it. ❝ […] if I want the sample sizes I can also easily get them via [1,7] or [1,8] from the sample size object returned … I suggest to use Sample.Nfoo()[["Sample size"]] instead. You can use it in all functions and the current indices are not carved in stone. If we introduce another variable before #7 or reorder variables in a future version of PowerTOST your code will not work any more.— Dif-tor heh smusma 🖖🏼 Довге життя Україна! Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮 Science Quotes |
d_labes ★★★ Berlin, Germany, 2018-01-23 16:03 (2512 d 21:34 ago) @ Helmut Posting: # 18270 Views: 5,133 |
|
Hi Helmut, hi ElMaestro, ❝ that’s strange... The answer to that problem: R is an interactive language and tries to save the user from keystrokes. Partial match of names is one of these tries. See Patrick Burns R-Inferno, Paragraph "8.1.20 partial matching can partially confuse". That means what we see in case of power.RSABE()$n is the value of power.RSABE()$nlast .Try: r2 <- sampleN.RSABE(CV=0.3, theta0=0.95, print=F) sampleN.TOST() doesn't have a return parameter which could be matched with n.Thefore we obtain correctly NULL. ❝ ... In the functions of The lazy author of the functions had forgotten to use the argument stringsAsFactors in creating the return data.frame. Have his butt's . Also for capitalizing Design.— Regards, Detlew |
ElMaestro ★★★ Denmark, 2018-01-23 16:26 (2512 d 21:10 ago) @ d_labes Posting: # 18272 Views: 5,080 |
|
What a fantastic little story. Mysteri solved. Thank you, Hötzi, for confirming that I am not completely hopeless. And thank you, d_labes, for confirming that I partially am Conclusion: from this point onwards I will do e.g. sampleN.RSABE(CV=0.3, theta0=0.95)$Sa etc.This code apparently illustrates the phenomenon: A=c("foo", "bar") B=c(1:2) C=data.frame(A,B) names(C)=c("Banana", "Boxer") C$B C$Bo C$Ba Now I can have peace of mind. Or closure , no pun intended. — Pass or fail! ElMaestro |
d_labes ★★★ Berlin, Germany, 2018-01-23 16:39 (2512 d 20:58 ago) @ ElMaestro Posting: # 18273 Views: 5,105 |
|
Dear ElMaestro, ❝ Conclusion: from this point onwards I will do e.g. to be on the safe side use sampleN.RSABE(CV=0.3, theta0=0.95, print=F)$`Sample size` .Ugly. But you get always what you want. Contrary to the Stones . — Regards, Detlew |
yjlee168 ★★★ Kaohsiung, Taiwan, 2018-01-23 18:47 (2512 d 18:49 ago) @ ElMaestro Posting: # 18279 Views: 5,123 |
|
Hi all, I think we can use str() to preview all factors' name in a data.frame. df <- sampleN.TOST(CV=0.3, theta0=0.95) There is no 'n' in df or sampleN.TOST(CV=0.3, theta0=0.95). So it returns NULL. If we do df['Sample size']
then it works. ❝ ...and therefore I can simply do this: ❝ ❝ (the n column is baptised as "Sample size" but is still accessible via $n) — All the best, -- Yung-jin Lee bear v2.9.2:- created by Hsin-ya Lee & Yung-jin Lee Kaohsiung, Taiwan https://www.pkpd168.com/bear Download link (updated) -> here |