similar to: lapply to apply a function using a vector

Displaying 20 results from an estimated 10000 matches similar to: "lapply to apply a function using a vector"

2010 Jun 23
1
Generation of binomial numbers using a loop
Dea'R' helpers I have following data - prob = c(0.1, 0.2, 0.3, 0.4, 0.5) frequency = c(100, 75, 45, 30, 25) no_trials = c(10, 8, 6, 4, 2) freq1 = rbinom(frequency[1], no_trials[1], prob[1]) freq2 = rbinom(frequency[2], no_trials[2], prob[2]) freq3 = rbinom(frequency[3], no_trials[3], prob[3]) freq4 = rbinom(frequency[4], no_trials[4], prob[4]) freq5 = rbinom(frequency[5],
2011 Jan 17
1
filling in datasets of differing lengths
g''day all, I need help with this please. I have a dataset of site names and years they were trapped in (a subset of it is bs, below) and a dataset of sites that actually caught animals (subsetted as brep, below). I need to add to brep, a row with a zero in the classcount column, for every year each site was trapped but caught nothing. so for Site 1, I need to add rows that say
2007 Nov 08
2
mapply, coxph, and model formula
Hello - I am wanting to create some Cox PH models with coxph (in package survival) using different datasets. The code below illustrates my current approach and problem with completing this. ### BEGIN R SAMPLE CODE ############################## library(survival) #Define a function to make test data makeTestDF <- function(n) { times <- sample(1:200, n, replace = TRUE) event
2023 Apr 08
1
Error message for infinite probability parameters in rbinom() and rmultinom()
On 08/04/2023 5:53 p.m., Martin Maechler wrote: >>>>>> Christophe Dutang >>>>>> on Sat, 8 Apr 2023 14:21:53 +0200 writes: > > > Dear all, > > > Using rmultinom() in a stochastic model, I found this function returns an error message 'NA in probability' for an infinite probability. > > > Maybe, a more
2023 Apr 08
1
Error message for infinite probability parameters in rbinom() and rmultinom()
>>>>> Christophe Dutang >>>>> on Sat, 8 Apr 2023 14:21:53 +0200 writes: > Dear all, > Using rmultinom() in a stochastic model, I found this function returns an error message 'NA in probability' for an infinite probability. > Maybe, a more precise message will be helpful when debugging. >> rmultinom(1, 3:5, c(1/2, 1/3,
2012 Jan 30
1
mgcv bam() with grouped binomial data
Hello, I'm trying to use the bam() function in the R mgcv package for a large set of grouped binary data. However, I have found that this function does not take data in the format of cbind(numerator, denominator) on the left hand side of the formula. As an example, consider the following dat1 <- data.frame(id=rep(1:6, each=3), num=rbinom(18, size=10, prob=0.8), den=rbinom(18, size=5,
2007 Aug 09
1
How to apply functions over rows of multiple matrices
Dear ExpRts, I would like to perform a function with two arguments over the rows of two matrices. There are a couple of *applys (including mApply in Hmisc) but I haven't found out how to do it straightforward. Applying to row indices works, but looks like a poor hack to me: sens <- function(test, gold) { if (any(gold==1)) { sum(test[which(gold==1)]/sum(which(gold==1))) } else NA }
2016 Mar 10
3
rmultinom.c error probability not sum to 1
Hi all, I should have given a better explanation of my problem. Here it is. I extracted from my code the bit that gives the error. Place this in a file called test.c #include <math.h> #include <R.h> #include <Rmath.h> #include <float.h> #include <R_ext/Print.h> int main(){ double prob[3] = {0.0, 0.0, 0.0}; double prob_tot = 0.; prob[0] = 0.3*dnorm(2, 0,
2016 Mar 10
2
rmultinom.c error probability not sum to 1
Dear all, I have a questions regarding using the c function rmultinom.c. I got the following error message "rbinom: probability sum should be 1, but is 0.999264" Which is thrown by: if(fabs((double)(p_tot - 1.)) > 1e-7) MATHLIB_ERROR(_("rbinom: probability sum should be 1, but is %g"), (double) p_tot); I understand my probabilities do not sum to one close enough. I
2023 Apr 08
1
Error message for infinite probability parameters in rbinom() and rmultinom()
Dear all, Using rmultinom() in a stochastic model, I found this function returns an error message 'NA in probability' for an infinite probability. Maybe, a more precise message will be helpful when debugging. > rmultinom(1, 3:5, c(1/2, 1/3, Inf)) Error in rmultinom(1, 3:5, c(1/2, 1/3, Inf)) : NA in probability vector > rmultinom(1, 3:5, c(1/2, 1/3, NA)) Error in rmultinom(1,
2007 Oct 17
3
how to repeat the results of a generated probabilities
hello, I want to simulate 200 times the mean of a joint probability (y1) and 200 times the mean of another joint distribution (y2), that is I'm expecting to get 200 means of y1 and 200 means of y2. y1 and y2 are probabilities that I calculate from the marginal prob. (z1 and z2 respectively) multiple by the conditional prob. (x1 and x2 respectively), which I generaterd from the binomial
2007 Oct 24
2
analytical solution to Sum of binominal distributed random numbers?
Frede Aakmann T?gersen wrote: > Perhaps > > http://stinet.dtic.mil/cgi-bin/GetTRDoc?AD=ADA266969&Location=U2&doc=GetTRDoc.pdf > > is something that you can use? Thanks a lot - that might help. Rainer > > > > Best regards > > Frede Aakmann T?gersen > Scientist > > > UNIVERSITY OF AARHUS > Faculty of Agricultural Sciences > Dept.
2009 Dec 08
4
lower.tail option in pnorm
Hi, I would have thought that these two constructions would produce the same result but they do not. Resp <- rbinom(10, 1, 0.5) Stim <- rep(0:1, 5) mm <- model.matrix(~ Stim) Xb <- mm %*% c(0, 1) ifelse(Resp, log(pnorm(Xb)), log(1 - pnorm(Xb))) pnorm(as.vector(Xb), lower.tail = Resp, log.p = TRUE) > ifelse(Resp, log(pnorm(Xb)), log(1 - pnorm(Xb))) [1] -0.6931472 -1.8410216
2008 Jul 09
3
rbinom for a matrix
I have a large matrix full of probabilities; I would like to convert each probability to a 1 or a 0 using rbinom. How can I do this on the entire matrix? The matrix was converted from a raster ArcMap dataset, so the matrix is essentially a map. Because of this, I have no column headings. Thanks! -- View this message in context: http://www.nabble.com/rbinom-for-a-matrix-tp18366867p18366867.html
2011 Feb 04
1
rbinom and probability
Hello compadRes, I'm developing a script that selects "cells" over a certain metabolic rate to kill them. A rate between 9 and 12 means that the cells are candidates for death. I'll show you what I mean: # a would be a vector of cell metabolic rates. a<-c(8, 7, 9, 6, 10, 11, 4, 5, 6) #now identify which cells will be candidates for death, namely those cells with metabolic
2013 May 23
1
sample(c(0, 1)...) vs. rbinom
Greetings.? My wife is teaching an introductory stat class at UC Davis.? The class emphasizes the use of simulations, rather than mathematics, to get insight into statistics, and R is the mandated tool.?? A student in the class recently inquired about different approaches to sampling from a binomial distribution.? I've appended some code that exhibits the idea, the gist of which is that using
2007 Oct 23
1
Sum of binominal distributed random numbers
Hi I have two vectors, prob and size, and I want to add the random deviates of these two, i.e. sum( sapply( 1:length(prob), function(n){ rbinom(1, size(n), prob(n) } ) ) My problem is that I have to do this for a large number of value combinations. Is there a faster way of doing this? Rainer
2008 May 28
5
"rbinom" not using probability of success right
I am trying to simulate a series of ones and zeros (1 or 0) and I am using "rbinom" but realizing that the number of successes expected is not accurate. Any advice out there. This is the example: N<-500 status<-rbinom(N, 1, prob = 0.15) count<-sum(status) 15 percent of 500 should be 75 but what I obtain from the "count" variable is 77 that gives the probability of
2007 Aug 10
1
[Fwd: Re: How to apply functions over rows of multiple matrices]
[Apologies to Gabor, who I sent a personal copy of the reply erroneously instead of posting to List directly] [...] > Perhaps what you really intend is to > take the average over those elements in each row of the first matrix which correspond to 1's in the second in the corresponding > row of the second. In that case its just: > > rowSums(newtest * goldstandard) /
2009 Jun 18
1
Running a function through a vector with the scalars as arguments
I have a vector of probabilities that I would like to feed to rbinom() and have it return a 1 or 0 for each element. I have tried simply using the entire vector as the argument hoping R would understand. Instead I get a scalar: theProbs <- runif(10,0,1) youreON <- rbinom(1, 1, theProbs) Next I tried using the apply function but I could not get that to work either: youreON2 <-