Output of chi-square as vector - statistics

I use the chi-square goodness of fit test with chisq.test(). Is there a way to save the output of this function as a vector, like:
test <- c(X-squared, df, p-value)?

The output of chisq.test is a list
x <- c(A = 20, B = 15, C = 25)
test_chi <- chisq.test(x)
str(test_chi)
List of 9
$ statistic: Named num 2.5
..- attr(*, "names")= chr "X-squared"
$ parameter: Named num 2
..- attr(*, "names")= chr "df"
$ p.value : num 0.287
$ method : chr "Chi-squared test for given probabilities"
$ data.name: chr "x"
$ observed : Named num [1:3] 20 15 25
..- attr(*, "names")= chr [1:3] "A" "B" "C"
$ expected : Named num [1:3] 20 20 20
..- attr(*, "names")= chr [1:3] "A" "B" "C"
$ residuals: Named num [1:3] 0 -1.12 1.12
..- attr(*, "names")= chr [1:3] "A" "B" "C"
$ stdres : Named num [1:3] 0 -1.37 1.37
..- attr(*, "names")= chr [1:3] "A" "B" "C"
- attr(*, "class")= chr "htest"
We can create a vector with some elements of the list
out <- c(test_chi$statistic,test_chi$parameter,test_chi$p.value)
out
X-squared df
2.5000000 2.0000000 0.2865048
Another option is to use the library broom
broom::tidy(test_chi)
# A tibble: 1 x 4
statistic p.value parameter method
<dbl> <dbl> <dbl> <chr>
1 2.5 0.287 2 Chi-squared test for given probabilities

Related

Forest plot for coxme models?

I have a mixed-effects coxme model and wanted to plot a forest graph(similar to ggforest for coxph). I'm slightly new to this so not sure how to plot this.
My df:
str(cats_52weeks)
'data.frame': 487 obs. of 50 variables:
$ Cat_ID : chr "Mor02" "Mor03" "Mor04" "Mor05" ...
$ Sex : chr "female" "male" "male" "male" ...
$ Weight_Initialcapture.kg. : num 2.45 5.1 5 4.9 5.95 4.4 4.8 5.5 5.6 5 ...
$ Study_region : chr "Central Kimberley" "Central Kimberley" "Central Kimberley" "Central Kimberley" ...
$ cat_density : num 0.17 0.17 0.17 0.17 0.17 0.17 0.17 0.17 0.17 0.17 ...
$ Study_length : Factor w/ 3 levels "short","baiting",..: 3 3 3 3 3 3 3 3 3
$ Rabbits_present : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Fox_present : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Dingo_present : Factor w/ 2 levels "no","yes": 2 2 2 2 2 2 2 2 2 2 ...
$ Habitat_type : Factor w/ 4 levels "Savannah","Desert",..: 1 1 1 1 1 1 1
$ Time2 : num 36 7 27 52 52 28 52 36 40 52 ...
$ Time1 : int 1 1 1 1 1 1 1 1 1 1 ...
$ Status : num 1 0 0 0 0 1 0 0 0 0 ...
$ age_category : Factor w/ 4 levels "tom","female",..: 4 1 1 1 1 3 1 1 1 1
And the model that I want to produce a forest plot is
m52_all <- coxme(Surv(cats_52weeks$Time2,cats_52weeks$Status) ~ Habitat_type+
Fox_present + Dingo_present + Rabbits_present + Weight_Initialcapture.kg. +
cat_density + (1|Study_region) + (1|Study_length),data=cats_52weeks)
Any help would be appreciated, thanks!!

How to resolve the "Object 'Note' not found error" when using rmst2 function from survRM2 package?

I aim to compare the restricted mean survival time between the two treatment groups in the Anderson dataset
Anderson dataset
Here is the structure of my data frame:
'data.frame': 42 obs. of 5 variables:
$ survt : num 19 17 13 11 10 10 9 7 6 6 ...
$ status: num 0 0 1 0 0 1 0 1 0 1 ...
$ sex : Factor w/ 2 levels "female","male": 1 1 1 1 1 1 1 1 1 1 ...
$ logwbc: 'labelled' num 2.05 2.16 2.88 2.6 2.7 2.96 2.8 4.43 3.2 2.31 ...
..- attr(*, "label")= Named chr "log WBC"
.. ..- attr(*, "names")= chr "logwbc"
$ rx : Factor w/ 2 levels "New treatment",..: 1 1 1 1 1 1 1 1 1 1 ...
..- attr(*, "label")= Named chr "Treatment"
.. ..- attr(*, "names")= chr "rx"
- attr(*, "codepage")= int 65001
I used the following code to compare the restricted mean survival time between the two treatment groups ("New treatment" vs. "Standard treatment):
time <- anderson$survt
status <- anderson$status
arm <- anderson$rx
rmst2(time, status, arm )
I get the following error:
Error in rmst2(time, status, arm) : object 'NOTE' not found
In addition: Warning messages:
1: In max(tt) : no non-missing arguments to max; returning -Inf
2: In min(ss[tt == tt0max]) :
no non-missing arguments to min; returning Inf
3: In max(tt) : no non-missing arguments to max; returning -Inf
4: In min(ss[tt == tt1max]) :
no non-missing arguments to min; returning Inf
Thanks
I converted the sex and rx variables from factor to numeric and the function worked.

Use # (Copy) as selection or filter on 2-d array

The J primitive Copy (#) can be used as a filter function, such as
k =: i.8
(k>3) # k
4 5 6 7
That's essentially
0 0 0 0 1 1 1 1 # i.8
The question is if the right-hand side of # is 2-d or higher rank shaped array, how to make a selection using #, if possible. For example:
k =: 2 4 $ i.8
(k > 3) # k
I got length error
What is the right way to make such a selection?
You can use the appropriate verb rank to get something like a 2d-selection:
(2 | k) #"1 1 k
1 3
5 7
but the requested axes have to be filled with 0s (or !.) to keep the correct shape:
(k > 3) #("1 1) k
0 0 0 0
4 5 6 7
(k > 2) #("1 1) k
3 0 0 0
4 5 6 7
You have to better define select for dimensions > 1 because now you have a structure. How do you discard values? Do you keep empty "cells"? Do you replace with 0s? Is structure important for the result?
If, for example, you only need the "values where" then just ravel , the array:
(,k > 2) # ,k
3 4 5 6 7
If you need to "replace where", then you can use amend }:
u =: 5 :'I. , 5 > y' NB. indices where 5 > y
0 u } k
0 0 0 0
0 5 6 7
z =: 3 2 4 $ i.25
u =: 4 :'I. , (5 > y) +. (0 = 3|y)' NB. indices where 5>y or 3 divides y
_999 u } z
_999 _999 _999 _999
_999 5 _999 7
8 _999 10 11
_999 13 14 _999
16 17 _999 19
20 _999 22 23

How do i create a Random Number Generator Function for haskell tetris game

my question is very straight forward, i want to have a function in my haskell code that works as following: i invoke it and it returns me a random Integer. I use it to generate my tetrominoes based on the generated numbers, any ideas?
EDIT, here's how my code that doenst works, looks right now
the codes would be something like:
Point is a data struc that i've made by myself, it doesnt really matters.
The problem with using unsafePerform is that it repeats the same number everytime
getRandomInt :: Int
getRandomInt = unsafePerformIO (getStdRandom (randomR (1, 7)))
getRandomTetromino :: [Point]
getRandomTetromino = getRT getRandomInt
getRT :: Int -> [Point]
getRT c
| c == 1 = [ Point 5 1 "T", Point 5 0 "T", Point 6 1 "T", Point 4 1 "T"]
| c == 2 = [ Point 5 1 "Z", Point 5 0 "Z", Point 4 1 "Z", Point 4 2 "Z"]
| c == 3 = [ Point 5 1 "I", Point 5 0 "I", Point 5 2 "I", Point 5 3 "I"]
| c == 4 = [ Point 5 1 "L", Point 6 1 "L", Point 4 1 "L", Point 6 0 "L"]
| c == 5 = [ Point 5 1 "J", Point 5 0 "J", Point 6 0 "J", Point 5 2 "J"]
| c == 6 = [ Point 5 1 "S", Point 5 0 "S", Point 6 0 "S", Point 4 1 "S"]
| c == 7 = [ Point 5 0 "O", Point 6 0 "O", Point 6 1 "O", Point 5 1 "O"]
You can do that using this :
import System.Random
import Control.Monad (replicateM)
main = replicateM 10 (randomIO :: IO Int) >>= print

Spliting a comma delimitted string into several columns and asigning 0 to nospace

In my data.frame a vector x containing text strings (with six values (from 0 to 100) separated by comma inside each string) in this format:
x[1] "3,2,4,34,2,9"
x[2] "45,,67,,,"
x[3] ",,,,99,"
Here is the link to the actual vector I am having problems with: x.cvs x.cvs
Unfortunately, the value of "0" is recorded as "an empty no space" between the two commas, or before the first comma, or after the last comma.
It would be great first to be able to transform it into:
x[1] "3,2,4,34,2,9"
x[2] "45,0,67,0,0,0"
x[3] "0,0,0,0,99,0"
But most importantly, I would like to split this vector into 6 different vectors x1, x2, x3, x4, x5, x6 and each of them to take the value from the string, and replace "no space" between commas with "0", for example, the result should be:
x1[3] 0
x6[2] 0
I think the strsplit() would have worked if there has been a value between commas, but since there is no value, not even an empty space, I am not sure what is the right way to proceed, without getting NAs.
I tried the following, but it does give me a lot of errors:
x<- as.character(x)
x <- gsub(",,", ",0,", x)
x <- gsub(", ,", ",0,", x)
splitx = do.call("rbind", (strsplit(x, ",")))
splitx = data.frame(apply(splitx, 2, as.numeric))
names(splitx) = paste("x", 1:6, sep = "")
I get errors...
In rbind(c("51", "59", "59", "60", "51", "51"), c("51", "59", "59", :
number of columns of result is not a multiple of vector length (arg 10994)
In apply(splitx, 2, as.numeric) : NAs introduced by coercion
Here are two alternatives to consider, depending on what you are actually expecting as your output.
The first option outputs a set of vectors, but I find that to be a little bit unnecessary and can quickly litter your workspace with lots of objects.
The second option, which I prefer, creates a convenient data.frame with each row representing one of the items from your vector "x".
Sample Data
x <- vector()
x[1] <- "3,2,4,34,2,9"
x[2] <- "45,,67,,,"
x[3] <- ",,,,99,"
Option 1
Names <- paste0("A", seq_along(x))
for (i in seq_along(x)) {
assign(Names[i], {Z <- scan(text=x[i], sep=","); Z[is.na(Z)] <- 0; Z})
}
A1
# [1] 3 2 4 34 2 9
A2
# [1] 45 0 67 0 0 0
A3
# [1] 0 0 0 0 99 0
Option 2
Z <- read.csv(text = x, header = FALSE)
Z[is.na(Z)] <- 0
Z
# V1 V2 V3 V4 V5 V6
# 1 3 2 4 34 2 9
# 2 45 0 67 0 0 0
# 3 0 0 0 0 99 0
Extracting values from a data.frame is as easy as specifying the desired rows and columns.
Z[1, 3]
# [1] 4
Z[2, 4]
# [1] 0
Z[3, c(1, 3, 5)]
# V1 V3 V5
# 3 0 0 99

Resources