# Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

# PythonFrom R to Python for an econometric analysis

#### Giord

##### New Coder
Hello everyone, since I'm new here I beg you pardon if this post does not fully respect the guidelines but I have some problems in conducting an econometric analysis with R, but since it does not work I decided to run it with Python. I'm conducting an analysis on cumulate prices and quantities of electric energy, and I have some problems in setting up my dataset and my model to do so, therefore I'm asking help in this forum. Basically, in my dataset there are 30 Excel files, in which there are the prices and the quantities of demanded and offered energy (each spreadsheet is a day, and for each day there are hourly demanded and offered prices) and I have to estimate the elasticity of supply and demand via regression. In doing this I havetwo problems: one with the dataset and the other one with the regression itself. The first problem that I have is that I must create in my dataset the same result that another member of my team obtained with his R code:

Code:
``````"File" <-rbind("File1", "File2", "File3",..., "File30")
bid <- filter("File", PURPOSE_CD=="BID")
bid <- bid %>%  arrange(BID_OFFER_DATE_DT, INTERVAL_NO, MERIT_ORDER_NO)
bid <- bid %>% group_by (BID_OFFER_DATE_DT, INTERVAL_NO) %>% mutate(cum=cumsum(QUANTITY_NO))
bid <- bid %>% mutate(lncum=log(cum), lnp=log(ENERGY_PRICE_NO))
bid_s <- bid %>% select(BID_OFFER_DATE_DT,INTERVAL_NO, MERIT_ORDER_NO, QUANTITY_NO, cum)
bid <- bid %>% mutate(id=paste(BID_OFFER_DATE_DT, INTERVAL_NO, sep="_"))
bid <- as.data.frame(filter(bid, (lncum>0)))
bid_wider <- bid %>% pivot_wider(names_from = id, values_from = lncum)
bid_wider <- bid_wider[, c(19:42, 18, 1:17)]
glimpse(bid_wider)
bid_g <- as.data.frame(filter(bid_wider, (ENERGY_PRICE_NO>0 & ENERGY_PRICE_NO<3000 )))
mat <- matrix( , nrow = 5, ncol = 24)``````

In addition, I have to run a
Python:
``for``
loop for my regression just like he did:

Code:
``````for (i in 1:24)
{
lm<- lm(bid_g[[i]] ~ bid_g[[25]], data=na.omit(bid_g))
mat[1:2,i] <- lm\$coefficients
sigma<- (t(lm\$residuals) %*% lm\$residuals)/length(lm\$residuals)
mat[3,i] <- sigma
varx <-vcov(lm)
mat[4,i] <- sqrt(varx[1,1])
mat[5,i] <- sqrt(varx[2,2])
}``````

and since I'm a beginner I have no idea if in Python I can do that (and how). Basically, I have already loaded the Excel files that I must merge in order to create my dataset but I have no idea how to arrange my dataframe and run an "iterated" regression in Python in the same way my teammate did it with R. Your help would be very appreciated, thanks in advance!

Thanks for sharing your problem with us.