cannot populate a vector with for loop - populate

I am trying to populate the empty vector with value from a for loop. However i am having trouble with. See below for my code....
i <- c("AL", "AK", "AZ", "AR")
file <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
mylist = list()
for i in unique(file$State){
count = 1
file <- file[grep(i[count], file$State),]
head(file$State)
file[,11] <- as.numeric(file[,11])
head(file)
R <- file[order(file[,2], na.last = TRUE),]
head(R)
Rsub <- R[,c(2,11)]
head(Rsub)
Rsub2 <- Rsub[order(Rsub[,2], na.last = TRUE),]
head(Rsub2,20)
Rsub2$Rank <- rank(Rsub2[,2], na.last=TRUE, ties.method="first")
Rsub2 <- Rsub2[,-2]
head(Rsub2,40)
su <- subset(Rsub2, Rsub2$Rank==20)
mylist <- su
count = count + 1
}
}
My final output has only values from one variable
> mylist
Hospital.Name Rank
59 D W MCMILLAN MEMORIAL HOSPITAL 20
Can somebody point to me where i am doing wrong?
Thanks
Upendra

I figured it out by myself....
file <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
data <- data.frame()
for (i in unique(file$State)){
#count = 1
file1 <- file[grep(i, file$State),]
head(file1$State)
file1[,11] <- as.numeric(file1[,11])
head(file1)
R <- file1[order(file1[,2], na.last = TRUE),]
head(R)
Rsub <- R[,c(2,11)]
head(Rsub)
Rsub2 <- Rsub[order(Rsub[,2], na.last = TRUE),]
head(Rsub2,20)
Rsub2$Rank <- rank(Rsub2[,2], na.last=TRUE, ties.method="first")
Rsub2 <- Rsub2[,-2]
head(Rsub2,40)
su <- subset(Rsub2, Rsub2$Rank==20)
data <- rbind(data,su)
}

Related

R Shiny - Extracting Anti-Diagonal elements in matrix using for-loops

I am trying to create an R Shiny app which can read matrix inputs and extract the anti-diagonal elements, however, I can't figure out why the codes don't work as the way I wanted.
Below are the sample codes:
library(shinyMatrix)
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(shinyjs)
library(rhandsontable)
library(matrixStats)
ui =
dashboardPage(
dashboardHeader(disable = TRUE),
dashboardSidebar(disable = TRUE),
dashboardBody(rHandsontableOutput("input1"),
br(),
rHandsontableOutput("input2"),
br(),
rHandsontableOutput("results")))
server = function (input, output, session) {
output$input1 = renderRHandsontable({
MAT = matrix(as.numeric(''), nrow = 3, ncol = 3,
dimnames = list(paste(1:3), paste(1:3)))
rhandsontable(MAT, width = "100%", height = "100%") %>%
hot_col(col = c(1:3), valign = 'htCenter', format = "0,0")
})
row_input <- reactive({
req(input$input1)
my_input_matrix <- as.matrix(hot_to_r(input$input1))
my_input_row<- as.matrix(hot_to_r(input$input1))
for(i in 1:3) {
my_input_row[i] = sum(my_input_matrix[,i])
}
row_input = matrix(my_input_row, nrow = 1, ncol = 3,
dimnames = list("Rowname", paste(1:3)))
row_input
})
output$input2 <- renderRHandsontable({
rhandsontable(row_input())
})
table <- reactive({
my_input_matrix <- as.data.frame(hot_to_r(input$input1))
my_input_row <- as.data.frame(hot_to_r(input$input2))
my_table <- as.data.frame(hot_to_r(input$input1),
hot_to_r(input$input2))
for(i in 1:3) {
for(j in 3:1) {
my_table[,1] <- my_input_matrix[j,i]
my_table[,2] <- my_input_matrix[i,j]
}
}
table = data.frame("A" = my_table[,1],
"B" = my_table[,2],
stringsAsFactors = FALSE,
check.names = FALSE)
table
})
output$results = renderRHandsontable({
rhandsontable(table())
})
}
shinyApp(ui, server)
Below is the sample inputs and outputs:
1st table is the input matrix
2nd table is a 1-row output matrix which shows the sum of each column of the 1st table(not sure if this causes the issue, so I'll just put it there)
3rd table is the output table produced by the codes
Here is the issue, I want the 3rd table to show the anti-diagonal elements 7-5-3 in column A and "reverse anti-diagonal" elements 3-5-7 in column B from the 1st table like below instead of the above (3-3-3- and 7-7-7).
Please help! Thanks!
Solved the issue by changing the codes to below:
for(i in 1:3) {
for(j in 3:1) {
my_table[,1] <- rev(my_input_matrix[i+(j-1)*3])[i]
my_table[,2] <- my_input_matrix[i+(j-1)*3][i]
}
}

ifelse and return in the for loop

I need a help for my syntax.
library(e1071)
priori <- function (I, N, M) {
a <- as.matrix(runif(I, min = 0.65, max = 1.70))
b <- as.matrix(runif(I, min = -2.80, max = 2.80))
c <- as.matrix(runif(I, min = 0.00, max = 0.35))
k <- c(rnorm(N*20/100, 0, 1), rnorm(N*80/100,0, 0.01))
M <- cbind(b,a,c)
data <- as.data.frame(rmvlogis(N, M, IRT = FALSE, link = "logit", z.vals = k))
print(data)}
This is my syntax which is generate data.
priori.list <- vector("list", 3)
names(priori.list) <- paste0("L", seq_along(priori.list))
priori.sum.list <- vector("list", 3)
for (i in 1:3) {
for (j in 1:100) {
priori.list$L1[[j]] <- priori(10,100, M="2PL")
priori.list$L2[[j]] <- priori(20,500, M="2PL")
priori.list$L3[[j]] <- priori(40,1000,M="3PL")
priori.sum.list [[i]][[j]] <- rowSums(priori.list[[i]][[j]])
print(kurtosis(priori.sum.list[[i]][[j]]))
if(skewness(priori.sum.list[[i]][[j]])>=-1 | skewness(priori.sum.list[[i]][[j]]>=1)
& kurtosis(priori.sum.list[[i]][[j]])>=-1 | kurtosis(priori.sum.list[[i]][[j]]>=1))
{NA}
else
{return(j=j-1)}}}
Then I do a data list from syntax. I want to create a loop according to the skewness and kurtosis coefficient, but I couldn't. My purpose: If the skewness and kurtosis coefficient is outside 1 and -1, save to list if not regenerate. Can You help me for correct syntax? Thank You.

Can I transform those seperate forcecasts into one for loop?

The data is in quarters and starts from 1955 till 2019, what I'm trying to do is obtaining the fixed horizon forecasts by increasing the sample period by one point incrementally, and repeating the estimation and forecasting process for this new sample period, and extract the year ahead (h=4) forecast. I want to forecast from the second quarter of 2017.
however I've tried using for loop but it just dosen't work, is it possible to condense this code into a for loop function
a <- ts(data$UK.GDP.UA.MP, start = c(1955,1), end = c(2016,4), frequency = 4)
aa <- auto.arima(a)
aa
forecast(aa,h = 4)
b <- ts(data$UK.GDP.UA.MP, start = c(1955,1), end = c(2017,1), frequency = 4)
bb <- auto.arima(a)
bb
forecast(bb,h = 4)
c <- ts(data$UK.GDP.UA.MP, start = c(1955,1), end = c(2017,2), frequency = 4)
cc <- auto.arima(a)
cc
forecast(cc,h = 4)
d<- ts(data$UK.GDP.UA.MP, start = c(1955,1), end = c(2017,3), frequency = 4)
dd <- auto.arima(d)
dd
forecast(dd,h = 4)
e <- ts(data$UK.GDP.UA.MP, start = c(1955,1), end = c(2017,4), frequency = 4)
ee <- auto.arima(e)
ee
forecast(ee,h = 4)
f <- ts(data$UK.GDP.UA.MP, start = c(1955,1), end = c(2018,1), frequency = 4)
ff <- auto.arima(f)
ff
forecast(ff,h = 4)
g <- ts(data$UK.GDP.UA.MP, start = c(1955,1), end = c(2018,2), frequency = 4)
gg <- auto.arima(g)
gg
forecast(gg,h = 4)
h <- ts(data$UK.GDP.UA.MP, start = c(1955,1), end = c(2018,3), frequency = 4)
hh <- auto.arima(h)
hh
forecast(hh,h = 4)
library(forecast)
ukgdp <- ts(data$UK.GDP.UA.MP, start=1955, frequency=4)
fc <- matrix(NA, ncol=4, nrow=8) %>% ts(start=c(2016,4), frequency=4)
colnames(fc) <- paste("h =",1:4)
for(i in seq(8)) {
ukgdp_train <- window(ukgdp, end = c(2016, 3+i))
fit <- auto.arima(ukgdp_train)
fc[i,] <- forecast(fit, h=4)$mean
}
Created on 2019-12-06 by the reprex package (v0.3.0)

Algorithm for market clearing

I've got discrete step functions for supply and demand. I'm searching for an algorithm to find the equilibrium price, The data are below in R, but a solution any language (or pseudo-code) is acceptable.
demand = data.frame(volume = c(8,2,3,1,1), price=c(1,2,3,4,5))
supply = data.frame(volume = c(3,2,4,2,3), price=c(5,4,3,2,1))
demand$volume <- cumsum(demand$volume)
supply$volume <- cumsum(supply$volume)
plot(demand, type="s")
lines(supply, type="s", col=3)
You need to take partial cumsum volumes from opposite ends of the price range.
demand_cum = (15, 7, 5, 2, 1)
supply_cum = ( 3, 5, 9, 11, 14)
This shows you total, cumulative demand & supply at each price.
Now can you spot the equilibrium?
I was looking into a similar problem and found this great description: https://www.youtube.com/watch?v=FYfbM56L-mE&ab_channel=31761-Renewablesinelectricitymarkets
You can motivate a similar analysis for your problem. Consider an LP formulation. Given the dual solution, you can find the market-clearing price as follows:
demand = data.frame(Type = "demand",Q = c(8,2,3,1,1), P=c(1,2,3,4,5))
supply = data.frame(Type = "supply",Q = c(3,2,4,2,3), P=c(5,4,3,2,1))
ds <- rbind(supply,demand)
By representing the problem from LP, do the following:
ds[ds$Type == "demand","Q"] <- ds[ds$Type == "demand","Q"]
ds[ds$Type == "supply","Q"] <- ds[ds$Type == "supply","Q"]
P_s <- ds[ds$Type == "supply","P"]
P_d <- ds[ds$Type == "demand","P"]
Q_s <- ds[ds$Type == "supply","Q"]
Q_d <- ds[ds$Type == "demand","Q"]
c_vec <- c(P_s,-P_d)
A_mat <- diag(length(c_vec))
b_vec <- c(Q_s,Q_d)
dir_1 <- rep("<=",length(b_vec))
A2_mat <- c(rep(1,length(Q_s)),rep(-1,length(Q_d)))
b2_vec <- 0
A_mat <- rbind(A_mat,A2_mat)
b_vec <- c(b_vec,b2_vec)
dir_1 <- c(dir_1,"=")
library(lpSolve)
sol <- lp ("min", c_vec, A_mat, dir_1, b_vec, compute.sens=TRUE)
price_mc <- sol$duals[nrow(ds) + 1] # extracts the dual, which corresponds to the price
In your example, the market-clearing price is $2.

Comparing Record Results and Double For Loop

I have a double loop that I not only don't like, but would take 14 days to run on my computer since it is going over 3200 records and 1090 variables at about .12 per iteration.
A smaller reproducible bit. It simply checks how many numbers are in the same column between two records, not including NA's. Then it attaches the results to the original data frame.
y <- data.frame(c(1,2,1,NA,NA),c(3,3,3,4,NA),c(5,4,5,7,7),c(7,8,7,9,10))
resultdf <- NULL
for(i in 1:nrow(y))
{
results <- NULL
for(j in 1:nrow(y))
{
results <- c(results,sum((y[i,]==y[j,]),na.rm=TRUE))
}
resultdf <- cbind(resultdf,results)
}
y <- cbind(y,resultdf)
I have repeat calculations that could possibly be avoided leaving about 7 days.
If I understand correctly, a few apply functions are in C that might be faster. I haven't been able to get any to work though. I'm also curious if there is a package that would run faster. Can anyone help speed up the calculation?
Thank you!
I have created data to your specifications, and using #BenBolker's suggestion about using a matrix:
> y <- matrix(sample(c(1:9, NA), 3200 * 1090, replace = TRUE),
+ nrow = 3200, ncol = 1090)
and compared the computation times for three different implementations:
f1 was suggested by #Andrei:
> f1 <- function(y)apply(y, 1, function(r1)
+ apply(y, 1, function(r2)sum(r1==r2, na.rm=TRUE)))
> system.time(r1 <- f1(y))
user system elapsed
523.51 0.77 528.73
f2 was suggested by #VincentZoonekynd:
> f2 <- function(y) {
+ f <- function(i,j) sum(y[i,] == y[j,], na.rm=TRUE)
+ d <- outer( 1:nrow(y), 1:nrow(y), Vectorize(f) )
+ return(d)
+ }
> system.time(r2 <- f2(y))
user system elapsed
658.94 1.96 710.67
f3 is a double loop over the upper triangle as suggested by #BenBolker. It is also a bit more efficient than your OP in that it pre-allocates the output matrix:
> f3 <- function(y) {
+ result <- matrix(NA, nrow(y), nrow(y))
+ for (i in 1:nrow(y)) {
+ row1 <- y[i, ]
+ for (j in i:nrow(y)) {
+ row2 <- y[j, ]
+ num.matches <- sum(row1 == row2, na.rm = TRUE)
+ result[i, j] <- num.matches
+ result[j, i] <- num.matches
+ }
+ }
+ return(result)
+ }
> system.time(r3 <- f3(y))
user system elapsed
167.66 0.08 168.72
So the double loop is the fastest of all three, although not as elegant and compact as the other two answers.
Here is another solution, using outer.
f <- function(i,j) sum(y[i,] == y[j,], na.rm=TRUE)
d <- outer( 1:nrow(y), 1:nrow(y), Vectorize(f) )
Indeed, you can use apply function. Given the earlier hint that a matrix works faster, I would try:
ym <- as.matrix(y)
resultdf <- apply(ym, 1, function(r1) apply(ym, 1, function(r2) sum(r1==r2, na.rm=TRUE)))
You can get rid of the inner loop (using the y and f3 from #flodel's answer):
ty <- t(y)
ix <- rep(1:nrow(y),each = ncol(y))
f4 <- function(y){
result <- matrix(0L, nrow(y), nrow(y))
for(r in 1:nrow(y))
result[r,] <- rowsum(as.numeric(ty == y[r,]), ix, na.rm = T)
result
}
> system.time(out <- f4(y))
user system elapsed
52.616 21.061 74.000
> system.time(out <- f3(y))
user system elapsed
244.751 0.136 244.954
>
It actually does an extra job of computing twice the same thing, but is still 5 times faster. You can make it yet another 4 times faster by using the inner workings of rowsum. See this question for an example.

Resources