-
Notifications
You must be signed in to change notification settings - Fork 0
/
failureModelTest.Rmd
139 lines (123 loc) · 3.87 KB
/
failureModelTest.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
---
title: "failureModelTest"
author: "GeoffRussell"
date: "2023-11-24"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
## Failure model test
This file is a small set of test data for testing failure model code
First we specify a set of PV panels or anything else installed over time
```{r data}
df<-tribble(
~panels, ~year,
5, "2000",
9, "2001",
20, "2002",
35, "2003",
40, "2004",
56, "2005",
59, "2006",
69, "2007",
)
```
```{r}
makeWeibullFail<-function(mtbf,k) {
lambda<-1/mtbf
function(n,p) {
(1-exp(-(lambda*(n-1))^k))*p
}
}
makeFail<-function(mtbf) {
lambda<-1/mtbf
function(n,p) {
(1-exp(-lambda*(n-1)))*p
}
}
#----------------------------------------------------------------------------------
# we make two functions, the normal exponential failure and the weibull failure
# with a parameter of 5.
#----------------------------------------------------------------------------------
failFun<-makeFail(5)
failWeibullFun<-makeWeibullFail(5,5)
nyears<-length(df$panels)
failed<-rep(0,nyears)
wfailed<-rep(0,nyears)
print(nyears)
df <- df %>% mutate(produced=panels-lag(panels))
df$produced[1]=df$panels[1]
for(i in 1:nyears) {
if (i<nyears) {
for(n in (i+1):nyears) {
failed[n]<-failed[n]+failFun(n,df$produced[i])
wfailed[n]<-wfailed[n]+failWeibullFun(n,df$produced[i])
cat(paste0("i,n,panels[i],failed[n]:",i,",",n,",P=",df$produced[i],",F=",failed[n],",WF=",wfailed[n],"\n"))
}
}
}
```
** Let's plot the failure function
```{r}
nyears<-100
failFun<-makeFail(40)
failFun<-makeWeibullFail(40,1)
failWeibull5Fun<-makeWeibullFail(40,5.3)
failWeibull3Fun<-makeWeibullFail(40,2.4)
failed<-rep(0,nyears)
w5failed<-rep(0,nyears)
w3failed<-rep(0,nyears)
df<-tibble(
panels=c(100,rep(0,nyears-1)),
produced=c(100,rep(0,nyears-1))
)
for(i in 1:nyears) {
if (i<nyears) {
for(n in (i+1):nyears) {
failed[n]<-failed[n]+failFun(n,df$produced[i])
w5failed[n]<-w5failed[n]+failWeibull5Fun(n,df$produced[i])
w3failed[n]<-w3failed[n]+failWeibull3Fun(n,df$produced[i])
#cat(paste0("i,n,panels[i],failed[n]:",i,",",n,",P=",df$produced[i],",F=",failed[n],",WF=",wfailed[n],"\n"))
}
}
}
dfout<-tibble(
expfail=failed,
w5fail=w5failed,
w3fail=w3failed,
x=seq(1,100)
)
dfout %>% ggplot()+
geom_line(aes(x=x,y=expfail),color="blue") +
geom_line(aes(x=x,y=w3fail),color="red")+
geom_line(aes(x=x,y=w5fail),color="orange")+
annotate('text',x=24,y=50,label="k=1",color="blue")+
annotate('text',x=22,y=25,label="k=2.4",color="red")+
annotate('text',x=32,y=12,label="k=5.3",color="orange")+
labs(x="Years",y="Percentage Failed",title="Various failure models with average panel life of 40 years,
Exponential (k=1),Weibull quality (k=3),Weibull best quality (k=5)")
```
Add some testing code to predict future growth
```{r}
library(modelr)
dfgw<-read_csv("GWByCountry.csv") %>% mutate(Country=str_replace(Country,"Total ",""))
countries<-dfgw$Country %>% unique
regionGW<-dfgw %>% filter(Country=="Australia") %>%
mutate(cumGWinstalled=GW) %>% select(cumGWinstalled,Year)
model<-lm(cumGWinstalled~poly(as.numeric(Year),2),regionGW)
modelGW<-bind_rows(tibble(Year=seq(2023,2050),cumGWinstalled=rep(0,2050-2023+1)),regionGW)
growthPrediction<-modelGW %>% add_predictions(model,var="Predicted") %>% arrange(Year)
growthPrediction %>% ggplot() +
geom_col(aes(x=Year,y=cumGWinstalled),fill="blue") +
geom_line(aes(x=Year,y=Predicted),color="red")
df2<-read_csv("SolarWaste/df2.csv") %>% pivot_wider(names_from=`State`,values_from=`GW`)
model<-lm(cumGWinstalled~poly(as.numeric(Year),2),df2[1:18,])
df2<-read_csv("SolarWaste/df2.csv")
df3 <- df2 %>% add_predictions(model,var="Predicted")
write_csv(df3,"df3.csv")
df3 %>% ggplot() +
geom_col(aes(x=Year,y=cumGWinstalled),fill="blue") +
geom_line(aes(x=Year,y=Predicted),color="red")
```