Sorry if this is a really basic question, but I just couldn't figure it out on my own.
I'm currently learning R for college and I'm trying to figure out how to loop over a range of numbers and input them into a distribution function, like so:
for (i in 0:3) {
dpois(i,2/3)
}
All I'm trying to accomplish here is for R to calculate a Poisson distribution for 0-3, with a lambda of 2/3.
R expects some additional input which I haven't quite figured out.
If I replace dpois with some other function (like print()), the loop works.
Any tips would be greatly appreciated.
Thanks!
R is already vectorized, and as such no for-loop is necessary.
dpois(0:3, 2/3) should do the trick.
Related
Hi I was wondering if anyone could help me as I'm new to R and struggling to work out how to write in equations or to find a function that is already ingrained in R.
So I am currently trying to create a probability density function that generates a random sample of 50,000 draws from the U(0,1) distribution by alternating the values of beta and micro in my formula, naturally as long as beta > 0
I think I should be starting with
m=50000 #where m is the number of draws
runif(m,0,1)
And here's where I get into a bit of a rut and hence I am wondering if anyone could help me out on how to do this.
Thanks in advance for the help.
I have an experimental design. I want to calculate its D-efficiency.
I thought R package AlgDesign could help. I found function optFederov which generates the design and - if the user wants - returns its D efficiency. However, I don't want to use optFederov to generate the design - I already have my design!
I tried eval.design(~.,mydesign). But the only metrics it gives me are: determinant, A, diagonality, and gmean.variances. Maybe there is a way to get from determinant or A to D-efficiency (I am not a mathematician, so I am not sure). Or maybe some other way to calculate D-efficiency "manually" so to say?
Thanks a lot for any hint!
I was working on a similar project. I found out this formula Deff = (|X'X|^(1/p))/ND in this link. Where X is the model matrix, p is the number of betas in you linear model and ND the number of runs your experiment has. You could just make a code like this and it will do the trick.
det(t(X)%*%X)^(1/beta)/(numRuns)
I tested the results using JMP for my project so I believe this is the correct formula
Determinant, the first result given by eval.design, is the D-efficiency.
I am working on clustering of variables in matlab. Two functions come in ClustOfVar package in R, called hcluster() and cutreevar().
I am good in Matlab and would like to use alternatives of hcluster() and cutreevar() in it.
Does Matlab has any inbuilt function which computes exactly same as hcluster() and cutreevar() does in R?
Need help.
Thanks
for heirarchical clustering you'll probably want to look at clusterdata. Note that you'll need the statistical toolbox for this function.
Just a warning, I started using R a day ago...my apologies if anything seems idiotically simple.
Right now im trying to have R take in a .txt file with acelerometer data of an impact and calculate a Head injury criterion test for it. The HIC test requires that curve from the data be integrated on a certain interval.
The equation is at the link below...i tried to insert it here as an image but it would not let me. Apparently i need some reputation points before it'll let me do that.
a(t) is the aceleration curve.
So far i have not had an issue generating a suitable curve in R to match the data. The loess function worked quite well, and is exactly the kind of thing i was looking for...i just have no idea how to integrate it. As far as i can tell, loess is a non-parametric regression so there is no way to determine the equation of the curve iteslf. Is there a way to integrate it though?
If not, is there another way to acomplish this task using a different function?
Any help or insighful comments would be very much appreciated.
Thanks in advance,
Wes
One more question though James, how can i just get the number without the text and error when using the integrate() function?
You can use the predict function on your loess model to create a function to use with integrate.
# using the inbuilt dataset "pressure"
plot(pressure,type="l")
# create loess object and prediction function
l <- loess(pressure~temperature,pressure)
f <- function(x) predict(l,newdata=x)
# perform integration
integrate(f,0,360)
40176.5 with absolute error < 4.6
And to extract the value alone:
integrate(f,0,360)$value
[1] 40176.5
I am not really sure about the difference between CDF (Cumulative Distribution Function) and ECDF (Empirical Cumulative Distribution Function) but I usually utilize a CDF plot to make observations about my data.
I have been using R recently and am desperately trying to find out how to plot a CDF and CCDF (Complementary CDF) of my data. All I could find was that R has ecdf but am not really sure if this is what I am looking for. Plotting an ECDF is as simple as:
plot.ecdf(data)
Does anyone know how to plot a CDF and CCDF of a dataset using R?
A CDF commonly requires closed form when you know or assume a distribution. An ECDF, on the other hand, is 'empiricial' as it comes from your data. I just answered a question about using ecdf() and Hmisc's Ecdf() here the other day.
More generally, you can search here using terms such as
[r] ecdf
in the search box to look for 'ecdf' within the R tag. At rseek.org, little comes up for 'ccdf'. Is that maybe just the same as one minus the ECDF? If so, Ecdf() in Hmisc can do it.
I hope this helps, if not please re-phrase your question as it is not quite clear exactly what you are looking for. Both ecdf() and Ecdf() are pretty featureful so make sure to read their help pages.