Linear Regression

#Create Loop for each Station
for(i in 2:ncol(data)) {
   x <- data[,1]
   y <- data[,i]
   mod <- lm(y ~ x)
   summary(mod)
   Statframe[i,] <- c(i,
                      summary(mod)[['coefficients']]['(Intercept)','Estimate'],
                      summary(mod)[['coefficients']]['x','Estimate'],
                      summary(mod)[['coefficients']]['x','Pr(>|t|)'])
}

#Remove first row (if empty)
Statframe <- Statframe[-1,]
rownames(Statframe) <- NULL

#Add Start- and End-Date and transform it to numerics
Overview$Start = data[1,1]
Overview$Ende= data[nrow(data),1]

Overview$NStart = as.numeric(Overview$Start)
Overview$NEnde = as.numeric(Overview$Ende)

#Export Results of Regression
Overview$Konstante = Statframe$Konstante
Overview$Trendwert = Statframe$Trendwert
Overview$Pvalue = Statframe$Pvalue

#Calculate Trend Values for beginning and end
Overview$Tline_Start = Overview$Trendwert * Overview$NStart + Overview$Konstante
Overview$Tline_Ende = Overview$Trendwert * Overview$NEnde + Overview$Konstante

#Difference in Percent
Overview$Veränderung = Overview$Tline_Ende / (Overview$Tline_Start / 100) - 100

```

### Linear Regression within a List of Dataframes

```
#Create Subsets
Temp_subs <- Map(function(nm) Temp[c("Year", "Day", nm)], names(Temp)[-(1:2)])

#Transform Df-list
library(reshape)
Temp_subs = lapply(Temp_subs, function(x) {
  cast(x, Year ~ Day)})

#Empty DF for Regression
Statframe = data.frame(Day=1, Konstante=1, Trendwert=1, Pvalue=1)[-1,]

#Loop for output
for (a  in names(Temp_subs)) {
  for(i in 2:ncol(Temp_subs1)) {
    #Regression-Model
    x <- Temp_subs1$Year
    y <- Temp_subsa[,i]
    mod <- lm(y ~ x)
    summary(mod)

    #Add Statistics to empty DF
    #Overview str(summary(mod))
    Statframe[i,] <- c(i,
                       summary(mod)'coefficients'['(Intercept)','Estimate'],
                       summary(mod)'coefficients'['x','Estimate'],
                       summary(mod)'coefficients'['x','Pr(>|t|)'])

    filename <- paste(a, "_Statistics.txt",  col="", sep="")
    write.table(Statframe, filename, row.names=TRUE, col.names=TRUE)
  }
}

```

### Stepwise cross validation

```
#Calibration Periode 1+2+3
Cal_Data1 <- rbind(Data_Periode1, Data_Periode2)
Cal_Data <- rbind(Cal_Data1, Data_Periode3)
rownames(Cal_Data) <- NULL

Cal_Temp1 <- rbind(AUG_Temp_Periode1, AUG_Temp_Periode2)
Cal_Temp <- rbind(Cal_Temp1, AUG_Temp_Periode3)
rownames(Cal_Temp) <- NULL
###############################

#Extract selected stations and pass it into formula
gridnames <- paste0("Cal_Data$", colnames(Cal_Data[,2:ncol(Cal_Data)]))
fmla <- as.formula(paste("Cal_Temp$Tmax ~ ", paste(gridnames, collapse= "+")))

#Perform Linear Regression
Regression <- lm(fmla)
summary(Regression)

#Optimize Modell
StepRegression = step(Regression,direction="both")

#Extract Stations for optimized Modell
finalmod = as.character(StepRegression$call)[2]

#Perform Linear Regression for optimized Modell
OptiRegression = lm(finalmod)

#Output
Regression_Summary= as.data.frame (summary(OptiRegression)$coefficient)
Regression_Summary$RSquared = summary(OptiRegression)$r.squared
Regression_Summary$AdjRSquared = summary(OptiRegression)$adj.r.squared

```

### Modelling after cross validation

```
gridnames = rownames(Regression_Summary)[-1]
gridnames = str_sub (gridnames, 10)

Modell <- ((as.matrix(Referenz[, gridnames]) %*%
                 Regression_Summary[-1, 1]) + Regression_Summary[1, 1])[,1]

Modell = as.data.frame (Modell)
rownames(Modell) <- NULL

```