R for loop to populate new variable - r

Dear Stackoverflow community,
I am at a loss with my for loop and I'm pretty sure it is a minor problem. I did browse through the loop questions that have already been asked...
Here is what I have so far:
xvals <- as.matrix(seq(1,10, 1))
I would like to create ovals based on ovals
yvals <- for (i in 1:nrow(xvals)){
p <- 2.69/(1+2.69)
if (i == 1){
yvals[i,] <- round(p, 2)}
else {
yvals[i,] <- round(1-(p)^i, 2)}
}
Unfortunately, this thing keeps throwing an error
Error in 1:nrow(xvals) : argument of length 0
When I change xvals to matrix, I get a different error:
Error in yvals[i, ] <- round(p, 2) :
incorrect number of subscripts on matrix

Without the loop
xvals <- 1:10
p <- 2.69/(1+2.69)
yvals <- 1-p^xvals
yvals[1] <- p
yvals <- round(yvals, 2)

I think you should first create empty matrix to achieve loop to work. Try this, maybe it helps you
xvals <- as.matrix(seq(1,10, 1))
xvals
yvals <- matrix(0, nrow=nrow(xvals),ncol=ncol(xvals))
yvals
for (i in 1:nrow(xvals)){
p <- 2.69/(1+2.69)
if (i == 1){
yvals[i,] <- round(p, 2)}
else {
yvals[i,] <- round(1-(p)^i, 2)}
}
yvals

Do you actually want a matrix or will a vector do?
yvals <- replicate(10,0)
p <- 2.69/(1+2.69)
for (i in 1:length(yvals)){
if (i == 1){
yvals[i] <- round(p, 2)
}
else {
yvals[i] <- round(1-(p)^i, 2)}
}
xvals <- 1:10
plot(xvals, yvals)

Related

how to solve error non-numeric argument to binary operation

I want to calculate the stock return but I got error in
return=function(x)
{
n=length(x)
x=matrix(x,nrow=n,ncol=1)
return_data=matrix(nrow=n-1,ncol=1)
for(i in 1:n-1)
{
return_data[i]=log(x[i+1,]/x[i,])
}
return_data
}
R_JSMR=return(JSMR)
Error in x[i + 1, ]/x[i, ] : non-numeric argument to binary operator
how do I solve this? please help me, thank you very much :)
The error is to have the for loop in 1:n-1 instead of 1:(n-1). Here is the function corrected and a vectorized version of it.
log_returns <- function(x) {
n <- length(x)
return_data <- matrix(nrow = n - 1, ncol = 1)
for(i in 1:(n-1)) {
return_data[i] <- log(x[i+1]/x[i])
}
return_data
}
log_returns2 <- function(x) {
return_data <- log(x[-1]/x[-length(x)])
matrix(return_data, ncol = 1)
}
JSMR <- 1:10
R_JSMR <- log_returns(JSMR)
R_JSMR2 <- log_returns2(JSMR)
identical(R_JSMR,R_JSMR2)
#[1] TRUE
If you don't need to return a matrix, here are two one-liners.
log_returns3 <- function(x) log(x[-1]/x[-length(x)])
log_returns4 <- function(x) diff(log(x))

How to for-loop over a custom function R?

I wrote this function which returns the probability that the value 1 does not appear in a random sample when iterated 1000 times for varying sample sizes.
bday.function <- function(sample.size){
x <- vector()
for (i in 1:1000){
x[i] <- !any(data.frame(table(sample(1:365, sample.size, replace=TRUE)))$Var1 == 1)
}
return(mean(x))
}
Now I want to use this function and another for-loop to calculate the probability for every sample size between 500 and 1500 and make a simple scatter plot of my results. Here is what I tried:
z <- vector()
for (i in 500:1500) {
z[i] <- bday.function(i)
return(plot(z))
}
Edit: when I run bday.function the output is number of TRUE values divided by the total (1000) TRUE/FALSE outcomes:
bday.function(750)
[1] 0.122
I would like to replicate this for sample sizes between 500 and 1500 to generate a simple scatter plot
Edit 2: Thanks to everybody for the help! Here's my final solution:
x <- vector(length = 1000)
for (i in 1:1000){
x[i] <- !any(sample(1:365, 500, replace=TRUE) == 1)
}
x
bday.function <- function(sample.size){
x <- vector(length= 1000)
for (i in 1:1000){
x[i] <- !any(sample(1:365, sample.size, replace=TRUE) == 1)
}
return(mean(x))
}
bday.function(750)
z <- vector(length = 1000)
tmp.index <- 500:1500
for (i in seq_along(tmp.index)) {
z[i] <- bday.function(tmp.index[i])
}
#Plot
plot(tmp.index, z, xlab = "sample size", ylab = "Probability of no birthdays")
As #JohnColeman pointed in his sage comment, your function can be slow. Try these changes on your code for the printing output. I have run only 60 sims as I need to complete other things:
#Function
bday.function <- function(sample.size){
x <- vector()
for (i in 1:1000){
x[i] <- !any(data.frame(table(sample(1:365, sample.size, replace=TRUE)))$Var1 == 1)
}
return(mean(x))
}
#Loop
z <- vector()
vec <- 500:1500
for (i in seq_along(vec)) {
z[i] <- bday.function(vec[i])
}
#Plot
plot(z)
Output:
Are you looking for something like this?
bday.function <- function(sample.size) {
mean(sapply(seq(1000), function(x)
+!any(sample(365, sample.size, replace = TRUE) == 1)))
}
x <- 500:1500
y <- sapply(x, bday.function)
plot(x, y, xlab = "sample size", ylab = "Probability of no birthdays")

Recreating the diag() function

I am currently trying to duplicate the diag() function in R to get a better understanding. Unfortunately, my solution is only useful for symmetric matrices.
diagnew <- function(x){
k <- c()
for(i in 1:nrow(x)){
k[i] <- x[i,i]
}
return(k)
}
I would be grateful for any tips that allow me to generalize my function.
The error comes from looping pass min(nrow, ncol).
diagnew <- function(x){
n <- min(nrow(x), ncol(x))
k <- vector(class(x[1,1]), n)
for(i in seq.int(n)){
k[i] <- x[i,i]
}
k
}
mat <- matrix(1:10, 5)
diag(mat)
diagnew(mat)
identical(diag(mat), diagnew(mat))
#[1] TRUE

how to get output from data frames in R code

I have the following code to analyze data sets:
library("Matrix")
Data <-list(c(2,3),c(3,2),c(2,2))
TheSizes=c(3,4)
n=2
dd=2
StdGrid <- function(Data,TheSizes)
{
SGrid <- list(
Values = Data,
Sizes = TheSizes
)
class(SGrid) <- append(class(SGrid), c("StdGrid","Moment"))
return(SGrid)
}
theObject=StdGrid
MHistogramC <- function(theObject,n,dd)
{
sizes <- theObject$Sizes
l <- length(sizes)
data <- theObject$Values
Xarray <- matrix(rep(0,l*n),ncol=n)
N <- matrix(rep(0,l*n),ncol=n)
Histo <- matrix(rep(0,l*n),ncol=n)
GrandX <- lapply(data,function(x) log(x))
minX <- rep(0,l)
maxX <- rep(0,l)
DeltaX <- rep(0,l)
for(i in 1:l){
minX[i] <- min(GrandX[[i]])
maxX[i] <- max(GrandX[[i]])
DeltaX[i] <- maxX[i]/n-minX[i]/n
}
nzero <- numeric()
for(j in 1:n){
for(i in 1:l){
Xarray[i,j] <- minX[i]+(j-1/2)*DeltaX[i]
N[i,j] <- length(which((GrandX[[i]] >= minX[i]+(j-1/2)*DeltaX[i]-DeltaX[i]) & (GrandX[[i]] <= minX[i]+(j-1/2)*DeltaX[i]+DeltaX[i])))
Histo[i,j] <- log(N[i,j])
}
if(min(Histo[,j]) > - 10000){
nzero <- c(nzero,j)
}
}
alpha <- rep(0,lnzero)
falpha <- rep(0,lnzero)
for(j in 1:length(nzero)){
fit <- lm(Xarray[,nzero[j]] ~ log(sizes/dd))
alpha[j] <- fit$coefficients[[2]]
fit2 <- lm(Histo[,nzero[j]] ~ log(sizes/dd))
falpha[j] <- -fit2$coefficients[[2]]
}
Result <- data.frame(alpha=alpha,falpha=falpha)
return(Result)
}
MHistogramU <- function(theObject,n,dd)
{
sizes <- theObject$Sizes
l <- length(sizes)
data <- theObject$Values
Xarray <- matrix(rep(0,l*n),ncol=n)
N <- matrix(rep(0,l*n),ncol=n)
Histo <- matrix(rep(0,l*n),ncol=n)
GrandX <- lapply(data,function(x) log(x))
minX <- rep(0,l)
maxX <- rep(0,l)
DeltaX <- rep(0,l)
for(i in 1:l){
minX[i] <- min(GrandX[[i]])
maxX[i] <- max(GrandX[[i]])
DeltaX[i] <- maxX[i]/n-minX[i]/n
}
nzero <- numeric()
for(j in 1:n){
for(i in 1:l){
Xarray[i,j] <- minX[i]+(j-1/2)*DeltaX[i]
N[i,j] <- length(which((GrandX[[i]] >= minX[i]+(j-1/2)*DeltaX[i]-sqrt(DeltaX[i])) & (GrandX[[i]] <= minX[i]+(j-1/2)*DeltaX[i]+sqrt(DeltaX[i]))))
Histo[i,j] <- log(N[i,j])
}
if(min(Histo[,j]) > - 10000){
nzero <- c(nzero,j)
}
}
alpha <- rep(0,lnzero)
falpha <- rep(0,lnzero)
for(j in 1:length(nzero)){
fit <- lm(Xarray[,nzero[j]] ~ log(sizes/dd))
alpha[j] <- fit$coefficients[[2]]
fit2 <- lm(Histo[,nzero[j]] ~ log(sizes/dd))
falpha[j] <- -fit2$coefficients[[2]]
}
Result <- data.frame(alpha=alpha,falpha=falpha)
return(Result)
}
Which compiles, but i don't get anything in return. If I try to print "Result" the console says that the object "Result" was not found.
The inputs are:
Data : is a list of vector/grids
TheSizes : is a vector
theObject : the data defined as the class 'StdGrid' (defined below);
n : the number of values of alpha to be calculated;
dd : the dimension of the physical support of the measure.
What can I do to see the data frame that the code is supposed to return?
That is because your code is just a bunch of functions which are not called at all. A function would return value only when the function is called, it won't call itself.
Now looking at your code, It's hard to deduce what you are trying to calculate/analyze, but assuming all other codes are correct, and all functions are coded perfectly, you need to add the following lines to view the result at the end of your code:
var_MHistogramU <- MHistogramU(theObject,n,dd)
var_MHistogramC <- MHistogramC(theObject,n,dd)
To view the result, simply print the variables.
print(var_MHistogramU)
print(var_MHistogramC)
Remember: Printing Result won't work as the variable result is a local variable for the function, which is inaccessible globally.

R: The standard errors are not reasonable when using mle together with pnorm

The values in vector a are seen as the true values and I want to estimate them using mle. I make 100 "disturbed" vectors from a by adding noise, N(0,sigma^2). For every disturbed vector I sort them in decreasing order. Say that the observed order is: x_1>x_4>x_5>x_3>x_2
We want to calculate the probability to observe this order and we do so by: P(X_1-X_4>0|a)*P(X_4-X_5>0|a)*P(X_5-X_3>0|a)*P(X_3-X_2>0|a) (we ignore the dependency), where X_1-X_4 ~ N(a[1]-a[4],2*std^2) and so on.
Usually when you use mle you use density function, but here we use pnorm. The problem I have after running the code below is that the standard errors are all the same (and way too large) for the estimates. What can I have done wrong?
R-code:
#library(stats4)
n <- 100
x <- mat.or.vec(n,5)
y <- mat.or.vec(n,5)
a <- c(100,100.5,100.75,100.7,100.25)
std <- sd(a)
Var <- std^2
for(i in 1:n){
y[i,] <- a+rnorm(5,mean=0,sd=std)
x[i,] <- sort(y[i,],decreasing = TRUE,index.return=TRUE)$ix
}
matris <- mat.or.vec(n,4)
sigma <- sqrt(2*Var)
fit <- function(f1,f2,f3,f4,f5){
for(i in 1:n){
for(j in 1:4){
P <- if(x[i,j] == 1) {f1} else if(x[i,j] == 2) {f2} else if(x[i,j] == 3) {f3} else if(x[i,j] == 4) {f4} else {f5}
Q <- if(x[i,j+1] == 1) {f1} else if(x[i,(j+1)] == 2) {f2} else if(x[i,(j+1)] == 3) {f3} else if(x[i,(j+1)] == 4) {f4} else {f5}
mu <- P-Q
matris[i,j] <- pnorm(0,mean=mu,sd=sigma,lower.tail=FALSE,log.p=TRUE)
}
}
-sum(matris)
}
mle.results <- mle(fit,start=list(f1=a[1],f2=a[2],f3=a[3],f4=a[4],f5=a[5]))
summary(mle.results)

Resources