R coding for random value with contraint [closed] - r

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I want to know R code for generating 1000 random values for x and y between 1 to 7 such that x<=y and all the numbers are identically distributed.

With float number
x <- c()
y <- c()
for (i in 1:1000){
x[i] <- runif(1, 1, 7)
y[i] <- runif(1, x[i], 7)
// print or do something you want here
}
With integer number
x <- c()
y <- c()
for (i in 1:1000){
x[i] <- sample(1:7, 1)
y[i] <- sample(x[i]:7, 1)
// print or do something you want here
}
You can try it.

Related

How to solve nonlinear equations in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
For example, I try to solve for x: 12*exp(-2x/4)=0.5, or x^2+3x^4+9x^5=10, is there any method in R to solve such equations?
Try with uniroot function
r1 <- uniroot(function(x) 12*exp(-x/2) - 0.5, c(-1000, 1000))
r2 <- uniroot(function(x) x^2 + 3*x^4 + 9*x^5 - 10, c(-1000, 1000))
r1$root
[1] 6.356108
r2$root
[1] 0.943561
You have to define:
a function of the type f(x) = 0 (and remove the second member of equality)
an interval within which to search for the solution

Need dataset for forecasting in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I need a non time-series dataset for evaluating various forecasting techniques in R. Please help me find a suitable dataset. I can't find any. The requirements are: one dependent variable and at least 4 continuous independent variables no factor or binary columns. Please help me out if you have such data.
You could make one yourself:
var1 <- runif(100, 10, 100)
var2 <- rnorm(100, 20, 3)
var3 <- runif(100, 0.1, 0.9)
var4 <- rnorm(100, 1000, 2000)
odds <- (var1 + var2 + 50 * var3 + var4 / 100)/100
dv <- rbinom(100, 1, odds/(1 + odds))
df <- data.frame(dv, var1, var2, var3, var4)

elegant way to write a function [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
What is a more elegant way to write the below function. I am trying to practice my function development skills, and I am simply trying to manually recreate the CIs of a linear model. I am very well aware of the confint(model) function, but still...
jad<-function(x, y) {
model<-lm(y~x)
std.err<-coef(summary(model))[, 2]
coef.model1<-coef(summary(model))[, 1]
upper.ci<-coef.model1+1.96*std.err
lower.ci<-coef.model1-1.96*std.err
print(upper.ci)
print(lower.ci)
}
What about the code below?
jad <- function(x, y) {
`colnames<-`(
coef(summary(lm(y ~ x)))[,1:2] %*% matrix(c(1, 1.96, 1, -1.96), nrow = 2),
c("upper.ci", "lower.ci")
)
}

Write a program to calculate P(X + Y + Z = k) for arbitrary discrete non-negative rv’s X, Y , and Z [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Write a program(use R) to calculate P(X + Y + Z = k) for arbitrary discrete non-negative rv’s X, Y , and Z.(rv, random variable)
This is an exercise from my book. I have no idea how to start.
Thank you for your time.
Here's a start
First, define three different lists, each containing 30 draws from unique binomial distributions (you can define X,Y, and Z as any discrete distribution you want):
X = rbinom(30, 10, 0.8)
Y = rbinom(30, 5, 0.5)
Z = rbinom(30, 8, 0.3)
Then, create a function that calculates the probability of drawing a certain number k from the added lists:
probability <- function(k) {
combine <- X+Y+Z
return(sum(combine==k)/length(combine))
}
An example call with k=14:
> probability(k=14)
[1] 0.2333333

Numerically Solving equations with Standard Normal CDF and PDF (Optimization) using R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
How do we go about numerically solving equations of the sort below using R?
Please note, this can be shown to be convex and there is a separate thread on this.
https://stats.stackexchange.com/questions/158042/convexity-of-function-of-pdf-and-cdf-of-standard-normal-random-variable
This question has been posted on the Mathematics Forum to get Closed Form or other Theoretical Approaches, but it seems numerically solutions are the way to go?
https://math.stackexchange.com/questions/2689251/solving-equations-with-standard-normal-cdf-and-pdf-optimization
You can use the build in optimize function to directly optimize the original function:
g <- function(x, xi) {
(xi * x + dnorm(xi * x) / pnorm(xi * x))
}
fun <- function(x, xi, K) {
K * g(x, xi) + (K - x) * g((K - x), xi)
}
optimize(fun, interval = c(0, 10), xi = 1, K = 1)
#> $minimum
#> [1] 1.173975
#>
#> $objective
#> [1] 1.273246
Your original problem f(x) = g(x) can be formulated as a root finding problem f(x) - g(x) = 0. You can then use the uniroot function to solve that. See ?uniroot for details.

Resources