myawesomepkg 0.1.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- myawesomepkg/TSAPY1/1 (A) Working with Numpy Arrays.py +1146 -0
- myawesomepkg/TSAPY1/1(B)Aggregation (1).py +319 -0
- myawesomepkg/TSAPY1/1(C) Broadcasting .py +328 -0
- myawesomepkg/TSAPY1/10-A_Load_stringr.py +77 -0
- myawesomepkg/TSAPY1/10-B_Forcats.py +70 -0
- myawesomepkg/TSAPY1/2(a) Comparison, Masking And Boolean Logic (1).py +497 -0
- myawesomepkg/TSAPY1/2(b)Fancy Indexing.py +594 -0
- myawesomepkg/TSAPY1/2(c) Sorting Arrays.py +528 -0
- myawesomepkg/TSAPY1/2(d) Structured Array.py +350 -0
- myawesomepkg/TSAPY1/3 (A) Handling Missing Data.py +1013 -0
- myawesomepkg/TSAPY1/4A_Merge_Joins.py +1209 -0
- myawesomepkg/TSAPY1/9A_Dplyr.py +85 -0
- myawesomepkg/TSAPY1/9B_Tidyr.py +71 -0
- myawesomepkg/TSAPY1/Aggregation_Groupin_Pivot_Filter_Vectorice_Time_Series.py +1999 -0
- myawesomepkg/TSAPY1/Combining_Joins.py +1209 -0
- myawesomepkg/TSAPY1/P4-1-different_distance_methods_(euclidean)_with_prediction,_test_score_and_confusion_matrix1.py +131 -0
- myawesomepkg/TSAPY1/P4-2-k_means_clustering_with_prediction,_test_score_and_confusion_matrix2.py +150 -0
- myawesomepkg/TSAPY1/Pract3_C.py +482 -0
- myawesomepkg/TSAPY1/Pract5_Data_Visualization.py +481 -0
- myawesomepkg/TSAPY1/Practical 6.py +860 -0
- myawesomepkg/TSAPY1/Practical No 1.py +148 -0
- myawesomepkg/TSAPY1/Practical No 2.py +115 -0
- myawesomepkg/TSAPY1/Practical No 3.py +168 -0
- myawesomepkg/TSAPY1/Practical No 4 A.py +233 -0
- myawesomepkg/TSAPY1/Practical No 4 B.py +137 -0
- myawesomepkg/TSAPY1/Practical No 5.py +52 -0
- myawesomepkg/TSAPY1/Practical No 6.py +29 -0
- myawesomepkg/TSAPY1/Practical No 7.py +67 -0
- myawesomepkg/TSAPY1/Practical No 8.py +108 -0
- myawesomepkg/TSAPY1/Print_R.py +123 -0
- myawesomepkg/TSAPY1/R_Graph.py +32 -0
- myawesomepkg/TSAPY1/Working_Ggplot.py +53 -0
- myawesomepkg/TSAPY1/__init__.py +0 -0
- myawesomepkg/TSAPY1/p1_2_pca_iris.py +141 -0
- myawesomepkg/TSAPY1/p2_1_find_s.py +78 -0
- myawesomepkg/TSAPY1/p2_bcandidate_elimination_algorithm_(1).py +85 -0
- myawesomepkg/TSAPY1/p3_1_least_square_regression.py +105 -0
- myawesomepkg/TSAPY1/p3_2_logistic_regression_algorithm.py +79 -0
- myawesomepkg/TSAPY1/p5_1_hierarchical_clustering.py +143 -0
- myawesomepkg/TSAPY1/p5_2_k_nearest_neighbour_algorithm.py +104 -0
- myawesomepkg/TSAPY1/p6_1_id3_algorithm_.py +199 -0
- myawesomepkg/TSAPY1/p7_1_ann_backpropagation_algorithm.py +116 -0
- myawesomepkg/TSAPY1/p7_2_bds_association_rule_mining.py +99 -0
- myawesomepkg/TSAPY1/p8_1_gaussian_naive_bayes_.py +97 -0
- myawesomepkg/TSAPY1/p8_2_naive_bayes_document_classifier.py +111 -0
- myawesomepkg/TSAPY1/p9_1bayesian_network.py +91 -0
- myawesomepkg/TSAPY1/p9_b_loess_regression.py +113 -0
- myawesomepkg/TSAPY1/p_1_test_and_train.py +98 -0
- myawesomepkg/TSAPY1/pract3A-B.py +3212 -0
- myawesomepkg/TSAPY1/practical_no_3.py +167 -0
- myawesomepkg/TSAPY1/practical_no_4.py +215 -0
- myawesomepkg/TSAPY1/practical_no_4b.py +78 -0
- myawesomepkg/TSAPY1/practical_no_5_ac_and_pca.py +39 -0
- myawesomepkg/TSAPY1/practical_no_6.py +37 -0
- myawesomepkg/TSAPY1/practical_no_7.py +69 -0
- myawesomepkg/TSAPY1/practical_no_8.py +79 -0
- myawesomepkg/TSAPY1/tsa_practical_no_1.py +287 -0
- myawesomepkg/TSAPY1/tsa_practical_no_2.py +121 -0
- myawesomepkg/__init__.py +1 -0
- myawesomepkg/core.py +2 -0
- myawesomepkg-0.1.8.dist-info/METADATA +17 -0
- myawesomepkg-0.1.8.dist-info/RECORD +64 -0
- myawesomepkg-0.1.8.dist-info/WHEEL +5 -0
- myawesomepkg-0.1.8.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
PRCTICAL 4 B
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def test_stationarity(dataFrame, var):
|
|
6
|
+
dataFrame['rollMean']=dataFrame[var].rolling(window=12).mean()
|
|
7
|
+
dataFrame['rollStd']=dataFrame[var].rolling(window=12).std()
|
|
8
|
+
|
|
9
|
+
from statsmodels.tsa.stattools import adfuller
|
|
10
|
+
import seaborn as sns
|
|
11
|
+
adfTest = adfuller(dataFrame[var], autolag='AIC')
|
|
12
|
+
stats=pd.Series(adfTest[0:4],index=['Test Statistic','p-value', '#lags used', 'number of observations used'])
|
|
13
|
+
print(stats)
|
|
14
|
+
|
|
15
|
+
for key, value in adfTest[4].items():
|
|
16
|
+
print('\t%s: %.3f' % (key, value))
|
|
17
|
+
|
|
18
|
+
sns.lineplot(data=dataFrame, x=dataFrame.index, y=var)
|
|
19
|
+
sns.lineplot(data=dataFrame, x=dataFrame.index, y='rollMean')
|
|
20
|
+
sns.lineplot(data=dataFrame, x=dataFrame.index, y='rollStd')
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
********
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
import pandas as pd
|
|
28
|
+
import numpy as np
|
|
29
|
+
|
|
30
|
+
#Reading the airline-passengers data
|
|
31
|
+
|
|
32
|
+
data = pd.read_csv('/content/drive/MyDrive/MScDS TSA/AirPassengers.csv', index_col='Month')
|
|
33
|
+
|
|
34
|
+
#Checking for some values of the data.
|
|
35
|
+
|
|
36
|
+
data.head()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
************
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
air_df=data[['Passengers']]
|
|
45
|
+
air_df.head()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
**********
|
|
51
|
+
|
|
52
|
+
air_df['shift']=air_df.Passengers.shift()
|
|
53
|
+
air_df['shiftDiff']=air_df.Passengers - air_df['shift']
|
|
54
|
+
air_df.head()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
**********
|
|
58
|
+
|
|
59
|
+
test_stationarity(air_df.dropna(),'shiftDiff')
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
***********
|
|
63
|
+
|
|
64
|
+
log_df=air_df[['Passengers']]
|
|
65
|
+
log_df['log']=np.log(log_df['Passengers'])
|
|
66
|
+
log_df.head()
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
************
|
|
70
|
+
|
|
71
|
+
test_stationarity(log_df,'log')
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
sqrt_df=air_df[['Passengers']]
|
|
76
|
+
sqrt_df['sqrt']=np.sqrt(air_df['Passengers'])
|
|
77
|
+
sqrt_df.head()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
********
|
|
81
|
+
|
|
82
|
+
test_stationarity(sqrt_df,'sqrt')
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
***********
|
|
86
|
+
|
|
87
|
+
cbrt_df=air_df[['Passengers']]
|
|
88
|
+
cbrt_df['cbrt']=np.cbrt(air_df['Passengers'])
|
|
89
|
+
cbrt_df.head()
|
|
90
|
+
|
|
91
|
+
***********
|
|
92
|
+
|
|
93
|
+
test_stationarity(cbrt_df,'cbrt')
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
************
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
log_df2=log_df[['Passengers','log']]
|
|
102
|
+
log_df2['log_sqrt']=np.sqrt(log_df['log'])
|
|
103
|
+
log_df2.head()
|
|
104
|
+
|
|
105
|
+
**********
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
test_stationarity(log_df2,'log_sqrt')
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
********
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
log_df2=log_df[['Passengers','log']]
|
|
116
|
+
log_df2['log_sqrt']=np.sqrt(log_df['log'])
|
|
117
|
+
log_df2['logShiftDiff']=log_df2['log_sqrt']-log_df2['log_sqrt'].shift()
|
|
118
|
+
log_df2.head()
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
*********
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
test_stationarity(log_df2.dropna(),'logShiftDiff')
|
|
126
|
+
|
|
127
|
+
*************88
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Aim: Implementing auto correlation and partial auto-correlation on timeseries
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# ACF plot of time series
|
|
5
|
+
from pandas import read_csv
|
|
6
|
+
from matplotlib import pyplot
|
|
7
|
+
#from statsmodels.graphics.tsaplots import plot_acf
|
|
8
|
+
from pandas.plotting import autocorrelation_plot
|
|
9
|
+
series = read_csv('/content/drive/MyDrive/MScDS TSA/daily-min-temperatures.csv', header=0, index_col=0,parse_dates=True, squeeze=True)
|
|
10
|
+
#plot_acf(series)
|
|
11
|
+
autocorrelation_plot(series)
|
|
12
|
+
pyplot.show()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
*********
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# zoomed-in ACF plot of time series
|
|
21
|
+
from pandas import read_csv
|
|
22
|
+
from matplotlib import pyplot
|
|
23
|
+
from statsmodels.graphics.tsaplots import plot_acf
|
|
24
|
+
series = read_csv('/content/drive/MyDrive/MScDS TSA/daily-min-temperatures.csv', header=0, index_col=0,parse_dates=True, squeeze=True)
|
|
25
|
+
plot_acf(series, lags=50)
|
|
26
|
+
pyplot.show()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
**************
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# PACF plot of time series
|
|
35
|
+
from pandas import read_csv
|
|
36
|
+
from matplotlib import pyplot
|
|
37
|
+
from statsmodels.graphics.tsaplots import plot_pacf
|
|
38
|
+
series = read_csv('/content/drive/MyDrive/MScDS TSA/daily-min-temperatures.csv', header=0, index_col=0,
|
|
39
|
+
parse_dates=True, squeeze=True)
|
|
40
|
+
plot_pacf(series, lags=50)
|
|
41
|
+
pyplot.show()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
***************8
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Aim: Perform autoregression on time series data
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# create and evaluate a static autoregressive model
|
|
5
|
+
from pandas import read_csv
|
|
6
|
+
from matplotlib import pyplot
|
|
7
|
+
from statsmodels.tsa.ar_model import AutoReg
|
|
8
|
+
from sklearn.metrics import mean_squared_error
|
|
9
|
+
from math import sqrt
|
|
10
|
+
# load dataset
|
|
11
|
+
series = read_csv('/content/drive/MyDrive/MScDS TSA/daily-min-temperatures.csv', header=0, index_col=0,parse_dates=True, squeeze=True)
|
|
12
|
+
# split dataset
|
|
13
|
+
X = series.values
|
|
14
|
+
train, test = X[1:len(X)-7], X[len(X)-7:]
|
|
15
|
+
# train autoregression
|
|
16
|
+
model = AutoReg(train,30)
|
|
17
|
+
model_fit = model.fit()
|
|
18
|
+
print('Lag: %s' % model_fit.ar_lags)
|
|
19
|
+
print('Coefficients: %s' % model_fit.params)
|
|
20
|
+
# make predictions
|
|
21
|
+
predictions = model_fit.predict(start=len(train), end=len(train)+len(test)-1, dynamic=False)
|
|
22
|
+
for i in range(len(predictions)):
|
|
23
|
+
print('predicted=%f, expected=%f' % (predictions[i], test[i]))
|
|
24
|
+
rmse = sqrt(mean_squared_error(test, predictions))
|
|
25
|
+
print('Test RMSE: %.3f' % rmse)
|
|
26
|
+
# plot results
|
|
27
|
+
pyplot.plot(test)
|
|
28
|
+
pyplot.plot(predictions, color='red')
|
|
29
|
+
pyplot.show()
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Aim: Forecasting using MA model.
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# correct forecasts with a model of forecast residual errors
|
|
5
|
+
from pandas import read_csv
|
|
6
|
+
from pandas import DataFrame
|
|
7
|
+
from pandas import concat
|
|
8
|
+
from statsmodels.tsa.ar_model import AutoReg
|
|
9
|
+
from matplotlib import pyplot
|
|
10
|
+
from sklearn.metrics import mean_squared_error
|
|
11
|
+
from math import sqrt
|
|
12
|
+
# load data
|
|
13
|
+
series = read_csv('/content/drive/MyDrive/MScDS TSA/daily-total-female-births-CA.csv', header=0, index_col=0, parse_dates=True,squeeze=True)
|
|
14
|
+
# create lagged dataset
|
|
15
|
+
values = DataFrame(series.values)
|
|
16
|
+
dataframe = concat([values.shift(1), values], axis=1)
|
|
17
|
+
dataframe.columns = ['t', 't+1']
|
|
18
|
+
print(dataframe)
|
|
19
|
+
X = dataframe.values
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
*******
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# split into train and test sets
|
|
26
|
+
X = dataframe.values
|
|
27
|
+
train_size = int(len(X) * 0.66)
|
|
28
|
+
train, test = X[1:train_size], X[train_size:]
|
|
29
|
+
train_X, train_y = train[:,0], train[:,1]
|
|
30
|
+
test_X, test_y = test[:,0], test[:,1]
|
|
31
|
+
# persistence model on training set
|
|
32
|
+
train_pred = [x for x in train_X]
|
|
33
|
+
# calculate residuals
|
|
34
|
+
train_resid = [train_y[i]-train_pred[i] for i in range(len(train_pred))]
|
|
35
|
+
# model the training set residuals
|
|
36
|
+
model = AutoReg(train_resid,20)
|
|
37
|
+
model_fit = model.fit()
|
|
38
|
+
window = len(model_fit.ar_lags)
|
|
39
|
+
coef = model_fit.params
|
|
40
|
+
# walk forward over time steps in test
|
|
41
|
+
history = train_resid[len(train_resid)-window:]
|
|
42
|
+
history = [history[i] for i in range(len(history))]
|
|
43
|
+
predictions = list()
|
|
44
|
+
for t in range(len(test_y)):
|
|
45
|
+
# persistence
|
|
46
|
+
yhat = test_X[t]
|
|
47
|
+
error = test_y[t] - yhat
|
|
48
|
+
# predict error
|
|
49
|
+
length = len(history)
|
|
50
|
+
lag = [history[i] for i in range(length-window,length)]
|
|
51
|
+
pred_error = coef[0]
|
|
52
|
+
for d in range(window):
|
|
53
|
+
pred_error += coef[d+1] * lag[window-d-1]
|
|
54
|
+
# correct the prediction
|
|
55
|
+
yhat = yhat + pred_error
|
|
56
|
+
predictions.append(yhat)
|
|
57
|
+
history.append(error)
|
|
58
|
+
print('predicted=%f, expected=%f' % (yhat, test_y[t]))
|
|
59
|
+
# error
|
|
60
|
+
rmse = sqrt(mean_squared_error(test_y, predictions))
|
|
61
|
+
print('Test RMSE: %.3f' % rmse)
|
|
62
|
+
# plot predicted error
|
|
63
|
+
pyplot.plot(test_y)
|
|
64
|
+
pyplot.plot(predictions, color='red')
|
|
65
|
+
pyplot.show()
|
|
66
|
+
|
|
67
|
+
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Aim: Forecasting using ARIMA model --TEMPERATURE
|
|
2
|
+
Time Series Forecasting With ARIMA Model in Python for Temperature Prediction.
|
|
3
|
+
|
|
4
|
+
1) Reading Time Series Data in Python using Pandas library
|
|
5
|
+
|
|
6
|
+
import pandas as pd
|
|
7
|
+
df=pd.read_csv('/content/drive/MyDrive/MScDS TSA/MaunaLoaDailyTemps.csv',index_col='DATE',parse_dates=True)
|
|
8
|
+
df=df.dropna()
|
|
9
|
+
print('Shape of data',df.shape)
|
|
10
|
+
df.head()
|
|
11
|
+
df
|
|
12
|
+
|
|
13
|
+
**********
|
|
14
|
+
|
|
15
|
+
df['AvgTemp'].plot(figsize=(12,5))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
*******
|
|
19
|
+
|
|
20
|
+
2) Checking for stationarity of time series model
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
from statsmodels.tsa.stattools import adfuller
|
|
24
|
+
def adf_test(dataset):
|
|
25
|
+
dftest = adfuller(dataset, autolag = 'AIC')
|
|
26
|
+
print("1. ADF : ",dftest[0])
|
|
27
|
+
print("2. P-Value : ", dftest[1])
|
|
28
|
+
print("3. Num Of Lags : ", dftest[2])
|
|
29
|
+
print("4. Num Of Observations Used For ADF Regression:", dftest[3])
|
|
30
|
+
print("5. Critical Values :")
|
|
31
|
+
for key, val in dftest[4].items():
|
|
32
|
+
print("\t",key, ": ", val)
|
|
33
|
+
adf_test(df['AvgTemp'])
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
*************
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
Auto Arima Function to select order of Auto Regression Model
|
|
40
|
+
|
|
41
|
+
pip install pmdarima
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
from pmdarima import auto_arima
|
|
46
|
+
import warnings
|
|
47
|
+
warnings.filterwarnings("ignore")
|
|
48
|
+
stepwise_fit=auto_arima(df['AvgTemp'],trace=True,suppress_warnings=True)
|
|
49
|
+
stepwise_fit.summary()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
************8
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
Split Your Dataset
|
|
57
|
+
|
|
58
|
+
print(df.shape)
|
|
59
|
+
train=df.iloc[:-30]
|
|
60
|
+
test=df.iloc[-30:]
|
|
61
|
+
print(train.shape,test.shape)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
from statsmodels.tsa.arima.model import ARIMA
|
|
68
|
+
model=ARIMA(train['AvgTemp'],order=(1,0,5))
|
|
69
|
+
model=model.fit()
|
|
70
|
+
model.summary()
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
**************
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
Check How Good Your Model Is
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
start=len(train)
|
|
83
|
+
end=len(train)+len(test)-1
|
|
84
|
+
pred=model.predict(start=start,end=end,typ='levels').rename('ARIMA Predictions')
|
|
85
|
+
print(pred)
|
|
86
|
+
pred.index=df.index[start:end+1]
|
|
87
|
+
pred.plot(legend=True)
|
|
88
|
+
test['AvgTemp'].plot(legend=True)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
***********8
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
Check your Accuracy Metric
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
from sklearn.metrics import mean_squared_error
|
|
101
|
+
from math import sqrt
|
|
102
|
+
test['AvgTemp'].mean()
|
|
103
|
+
rmse=sqrt(mean_squared_error(pred,test['AvgTemp']))
|
|
104
|
+
print(rmse)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
1. Print in R
|
|
2
|
+
r
|
|
3
|
+
Copy
|
|
4
|
+
Edit
|
|
5
|
+
print("Hello Boss!")
|
|
6
|
+
🔹 2. Comments in R
|
|
7
|
+
r
|
|
8
|
+
Copy
|
|
9
|
+
Edit
|
|
10
|
+
# This is a single-line comment
|
|
11
|
+
🔹 3. Variables in R
|
|
12
|
+
r
|
|
13
|
+
Copy
|
|
14
|
+
Edit
|
|
15
|
+
x <- 10
|
|
16
|
+
y <- "Data"
|
|
17
|
+
🔹 4. Concatenate Elements
|
|
18
|
+
r
|
|
19
|
+
Copy
|
|
20
|
+
Edit
|
|
21
|
+
v <- c(1, 2, 3, 4)
|
|
22
|
+
print(v)
|
|
23
|
+
🔹 5. Multiple Variables
|
|
24
|
+
r
|
|
25
|
+
Copy
|
|
26
|
+
Edit
|
|
27
|
+
a <- 5
|
|
28
|
+
b <- 10
|
|
29
|
+
c <- a + b
|
|
30
|
+
print(c)
|
|
31
|
+
🔹 6. Variable Names
|
|
32
|
+
r
|
|
33
|
+
Copy
|
|
34
|
+
Edit
|
|
35
|
+
user_name <- "Boss"
|
|
36
|
+
user_age <- 25
|
|
37
|
+
🔹 7. Data Types
|
|
38
|
+
r
|
|
39
|
+
Copy
|
|
40
|
+
Edit
|
|
41
|
+
num <- 10.5 # Numeric
|
|
42
|
+
str <- "Hello" # Character
|
|
43
|
+
bool <- TRUE # Logical
|
|
44
|
+
vec <- c(1, 2, 3) # Vector
|
|
45
|
+
🔹 8. Strings
|
|
46
|
+
r
|
|
47
|
+
Copy
|
|
48
|
+
Edit
|
|
49
|
+
name <- "R Programming"
|
|
50
|
+
paste("Welcome to", name)
|
|
51
|
+
🔹 9. Boolean
|
|
52
|
+
r
|
|
53
|
+
Copy
|
|
54
|
+
Edit
|
|
55
|
+
is_true <- TRUE
|
|
56
|
+
is_false <- FALSE
|
|
57
|
+
🔹 10. Operators
|
|
58
|
+
r
|
|
59
|
+
Copy
|
|
60
|
+
Edit
|
|
61
|
+
a <- 10
|
|
62
|
+
b <- 3
|
|
63
|
+
a + b # Addition
|
|
64
|
+
a > b # Comparison
|
|
65
|
+
a == b # Equal
|
|
66
|
+
a %% b # Modulus
|
|
67
|
+
🔹 11. If Else
|
|
68
|
+
r
|
|
69
|
+
Copy
|
|
70
|
+
Edit
|
|
71
|
+
x <- 10
|
|
72
|
+
if (x > 5) {
|
|
73
|
+
print("Greater than 5")
|
|
74
|
+
} else {
|
|
75
|
+
print("5 or less")
|
|
76
|
+
}
|
|
77
|
+
🔹 12. List
|
|
78
|
+
r
|
|
79
|
+
Copy
|
|
80
|
+
Edit
|
|
81
|
+
my_list <- list(name="Boss", age=25, scores=c(90, 85))
|
|
82
|
+
print(my_list)
|
|
83
|
+
🔹 13. Matrices
|
|
84
|
+
r
|
|
85
|
+
Copy
|
|
86
|
+
Edit
|
|
87
|
+
matrix_data <- matrix(1:6, nrow=2, ncol=3)
|
|
88
|
+
print(matrix_data)
|
|
89
|
+
🔹 14. Data Frames
|
|
90
|
+
r
|
|
91
|
+
Copy
|
|
92
|
+
Edit
|
|
93
|
+
df <- data.frame(Name=c("A", "B"), Age=c(20, 25))
|
|
94
|
+
print(df)
|
|
95
|
+
🔹 15. Functions
|
|
96
|
+
r
|
|
97
|
+
Copy
|
|
98
|
+
Edit
|
|
99
|
+
add_numbers <- function(x, y) {
|
|
100
|
+
return(x + y)
|
|
101
|
+
}
|
|
102
|
+
🔹 16. Call a Function
|
|
103
|
+
r
|
|
104
|
+
Copy
|
|
105
|
+
Edit
|
|
106
|
+
result <- add_numbers(5, 3)
|
|
107
|
+
print(result)
|
|
108
|
+
🔹 17. Global Variable
|
|
109
|
+
r
|
|
110
|
+
Copy
|
|
111
|
+
Edit
|
|
112
|
+
x <- 5
|
|
113
|
+
my_func <- function() {
|
|
114
|
+
x <<- 10 # Modify global x
|
|
115
|
+
}
|
|
116
|
+
my_func()
|
|
117
|
+
print(x)
|
|
118
|
+
🔹 18. Vectors
|
|
119
|
+
r
|
|
120
|
+
Copy
|
|
121
|
+
Edit
|
|
122
|
+
my_vector <- c(1, 2, 3, 4, 5)
|
|
123
|
+
print(my_vector)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
1. Line Plot
|
|
2
|
+
r
|
|
3
|
+
Copy
|
|
4
|
+
Edit
|
|
5
|
+
x <- c(1, 2, 3, 4, 5)
|
|
6
|
+
y <- c(2, 4, 6, 8, 10)
|
|
7
|
+
|
|
8
|
+
plot(x, y, type="l", col="blue", main="Line Plot", xlab="X-axis", ylab="Y-axis")
|
|
9
|
+
🔹 2. Scatter Plot
|
|
10
|
+
r
|
|
11
|
+
Copy
|
|
12
|
+
Edit
|
|
13
|
+
x <- c(1, 2, 3, 4, 5)
|
|
14
|
+
y <- c(5, 3, 6, 2, 7)
|
|
15
|
+
|
|
16
|
+
plot(x, y, main="Scatter Plot", xlab="X", ylab="Y", col="red", pch=19)
|
|
17
|
+
🔹 3. Pie Chart
|
|
18
|
+
r
|
|
19
|
+
Copy
|
|
20
|
+
Edit
|
|
21
|
+
slices <- c(10, 20, 30, 40)
|
|
22
|
+
labels <- c("A", "B", "C", "D")
|
|
23
|
+
|
|
24
|
+
pie(slices, labels=labels, main="Pie Chart")
|
|
25
|
+
🔹 4. Bar Chart
|
|
26
|
+
r
|
|
27
|
+
Copy
|
|
28
|
+
Edit
|
|
29
|
+
values <- c(5, 10, 15, 20)
|
|
30
|
+
names <- c("A", "B", "C", "D")
|
|
31
|
+
|
|
32
|
+
barplot(values, names.arg=names, col="green", main="Bar Chart", ylab="Values")
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
✅ Step 1: Install & Load ggplot2
|
|
2
|
+
r
|
|
3
|
+
Copy
|
|
4
|
+
Edit
|
|
5
|
+
install.packages("ggplot2") # Run once
|
|
6
|
+
library(ggplot2)
|
|
7
|
+
✅ Step 2: Sample Data
|
|
8
|
+
r
|
|
9
|
+
Copy
|
|
10
|
+
Edit
|
|
11
|
+
data <- data.frame(
|
|
12
|
+
category = rep(c("A", "B", "C"), each=4),
|
|
13
|
+
subcat = rep(c("X", "Y"), times=6),
|
|
14
|
+
value = c(4, 7, 6, 9, 5, 3, 8, 4, 7, 5, 6, 2)
|
|
15
|
+
)
|
|
16
|
+
✅ Step 3: Basic ggplot
|
|
17
|
+
r
|
|
18
|
+
Copy
|
|
19
|
+
Edit
|
|
20
|
+
ggplot(data, aes(x=subcat, y=value)) +
|
|
21
|
+
geom_bar(stat="identity", fill="steelblue") +
|
|
22
|
+
ggtitle("Basic Bar Chart")
|
|
23
|
+
✅ Step 4: Facets
|
|
24
|
+
r
|
|
25
|
+
Copy
|
|
26
|
+
Edit
|
|
27
|
+
ggplot(data, aes(x=subcat, y=value)) +
|
|
28
|
+
geom_bar(stat="identity", fill="tomato") +
|
|
29
|
+
facet_wrap(~ category) +
|
|
30
|
+
ggtitle("Faceted by Category")
|
|
31
|
+
✅ Step 5: Geometric Objects
|
|
32
|
+
r
|
|
33
|
+
Copy
|
|
34
|
+
Edit
|
|
35
|
+
ggplot(data, aes(x=subcat, y=value, fill=category)) +
|
|
36
|
+
geom_bar(stat="identity", position="dodge") + # Bar chart
|
|
37
|
+
geom_point(aes(color=category), size=3, shape=21) + # Add points
|
|
38
|
+
ggtitle("Geometric Objects: Bars + Points")
|
|
39
|
+
✅ Step 6: Position Adjustment
|
|
40
|
+
r
|
|
41
|
+
Copy
|
|
42
|
+
Edit
|
|
43
|
+
ggplot(data, aes(x=subcat, y=value, fill=category)) +
|
|
44
|
+
geom_bar(stat="identity", position=position_dodge(width=0.7)) +
|
|
45
|
+
ggtitle("Position: Dodge for Side-by-Side Bars")
|
|
46
|
+
✅ Step 7: Coordinate System (Flip Axis)
|
|
47
|
+
r
|
|
48
|
+
Copy
|
|
49
|
+
Edit
|
|
50
|
+
ggplot(data, aes(x=subcat, y=value, fill=category)) +
|
|
51
|
+
geom_bar(stat="identity") +
|
|
52
|
+
coord_flip() +
|
|
53
|
+
ggtitle("Flipped Coordinates")
|
|
File without changes
|